{{announcement.body}}
{{announcement.title}}

Day 2 of 30 - Ruby Coding Challenge - Ugly Prime Algorithm

DZone 's Guide to

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

· Web Dev Zone ·
Free Resource

Hey!

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:

Ruby
 




x


 
1
def count_prime_number_version_1(array)
2
    # magic will happen here
3
end
4
 
          
5
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
6
puts count_prime_number_version_1(array)


Just for clarity, I’m going to create a variable, which indicates the total count:

Ruby
 




xxxxxxxxxx
1


 
1
def count_prime_number_version_1(array)
2
  prime_count = 0
3
    # a loop expression here
4
  return prime_count
5
end
6
 
          
7
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
8
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

Ruby
 




xxxxxxxxxx
1


 
1
def count_prime_number_version_1(array)
2
  prime_count = 0
3
  for item in array
4
        
5
  end
6
  return prime_count
7
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

Ruby
 




xxxxxxxxxx
1


 
1
def count_prime_number_version_1(array)
2
  prime_count = 0
3
  for item in array
4
        next if item == 1
5
  end
6
  return prime_count
7
end


How can we validate in Ruby if a number is divided by another number, resulting in a remainder zero?

With percent %:

Ruby
 




xxxxxxxxxx
1


 
1
# checking if the remainder value is zero when dividing an item by count
2
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
Ruby
 




xxxxxxxxxx
1
12


 
1
is_prime = true
2
 
          
3
number = item - 1
4
while number > 1
5
  if item % number != 0
6
    number = number - 1
7
  else
8
    is_prime = false
9
    break
10
  end
11
end
12
 
          


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:

Ruby
 




xxxxxxxxxx
1
25


 
1
def count_prime_number_version_1(array)
2
  prime_count = 0
3
  for item in array
4
    next if item == 1
5
    is_prime = true
6

7
    number = item - 1
8
    while number > 1
9
      if item % number == 0
10
        is_prime = false
11
        break
12
      else
13
        number = number - 1
14
      end
15
    end
16

17
    if is_prime
18
      prime_count = prime_count + 1
19
    end
20
  end
21
  return prime_count
22
end
23

24
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
25
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!

Twitter Youtube Instagram Linkedin

Topics:
algorithm, coding, language basics, programming, programming language, ruby, tutorial for beginners

Published at DZone with permission of Alexandre Gama . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}