Day 2 of 30  Ruby Coding Challenge  Ugly Prime Algorithm
Day 2 of 30  Ruby Coding Challenges in 30 Days. Continuing to stretch our arms and legs to the long run, I’m going to solve a simple and common algorithm: How Many Prime Numbers Are in a Given Array
Join the DZone community and get the full member experience.
Join For FreeHey!
Day 2 of 30  Ruby Coding Challenges in 30 Days. This is the post version of the Youtube video.
Continuing to stretch our arms and legs to the long run, I’m going to solve a simple and common algorithm:
How Many Prime Numbers Are in a Given Array
How does my brain work when it comes to solving algorithms? Just get the job done!
 Forget about good practices
 Forget about code design
 Especially forget performance
I really love to respect my brain because, well, it can be against me.
Today I’ll solve this problem with poor design and you’ll get the feeling that we should not be friends anymore.
But I promise that I’m going to write AT LEAST four versions of this Algorithm and maybe you might reconsider our friendship.
Let’s start this conversation with refreshing our brain.
What is a Prime Number
Short answer:
A natural number greater than 1 that is evenly divided only by 1 and itself.
Wikipedia has a really sexy answer.
Going through some examples:
 1 is prime.
 2 is prime.
 3 is prime.
 4 is not prime. It’s divisible by 2.
 5 is prime.
 6 is not prime. It’s divisible by 2 and 3.
 7 is prime.
 8 is not prime. It’s divisible by 2 and 4.
 9 is not prime. It’s divisible by 3.
 10 is not prime. It’s divisible by 2 and 5.
Yep, you got it.
Prime Number Algorithm in Ruby
Are you familiar with Ruby? If so, then probably you’re already aware of the Prime library. This is an unfair advantage.
I’m going to talk about that later but for now, let’s stick with the old and good manual process.
It’s time to create the code skeleton. A method waiting for an array:
def count_prime_number_version_1(array)
# magic will happen here
end
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
puts count_prime_number_version_1(array)
Just for clarity, I’m going to create a variable, which indicates the total count:
xxxxxxxxxx
def count_prime_number_version_1(array)
prime_count = 0
# a loop expression here
return prime_count
end
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
puts count_prime_number_version_1(array)
It’s time to go through the array and ask for each item if they are prime or not
xxxxxxxxxx
def count_prime_number_version_1(array)
prime_count = 0
for item in array
end
return prime_count
end
We know that 1 is not prime, so why not just ignore it?
In Ruby, we use next to say: “Hey Ruby, forget this item and jump to the next item”
Are you coming from Java, Python or Javascript? That’s the same with continue
xxxxxxxxxx
def count_prime_number_version_1(array)
prime_count = 0
for item in array
next if item == 1
end
return prime_count
end
How can we validate in Ruby if a number is divided by another number, resulting in a remainder zero?
With percent %:
xxxxxxxxxx
# checking if the remainder value is zero when dividing an item by count
item % count == 0
Ok, now we can write a while loop that
 given an item
 check if there’s a number that divides the item, remaining in zero
xxxxxxxxxx
is_prime = true
number = item  1
while number > 1
if item % number != 0
number = number  1
else
is_prime = false
break
end
end
Notice that:
 we’re using is_prime to indicate when an item is, well, prime
 we’re using break to stop the while loop once the item is divided by a number, which already means that it’s not prime
It’s time to risk our virtual friendship. The complete code:
xxxxxxxxxx
def count_prime_number_version_1(array)
prime_count = 0
for item in array
next if item == 1
is_prime = true
number = item  1
while number > 1
if item % number == 0
is_prime = false
break
else
number = number  1
end
end
if is_prime
prime_count = prime_count + 1
end
end
return prime_count
end
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
puts count_prime_number_version_1(array)
What do I want to prove?
 I write terrible code at first and I’m not even sorry
 I don’t worry about design, performance, etc on the first version. Just get the job done (and lose some friends on the way)
 For beginners, it’s probably a good way to go through this process
As I said, this algorithm will become beautiful (hopefully), or at least better in the upcoming video and we’ll be friends again.
See you!
Published at DZone with permission of Alexandre Gama. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending

Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations

Top 10 Pillars of Zero Trust Networks

Getting Started With the YugabyteDB Managed REST API

Operator Overloading in Java
Comments