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

Day 5 of 30 Coding Challenge: Sum Prime Numbers Functionally

DZone 's Guide to

Day 5 of 30 Coding Challenge: Sum Prime Numbers Functionally

We’re going to solve the problem of getting the sum of prime numbers in a given array using functional programming with Ruby

· Web Dev Zone ·
Free Resource

Hey!

Day 5 of 30 of this Ruby Coding Challenge series, and this is the post version of the Youtube video.

Today, we’ll explore the following problem:

Generate a Sum of Prime Numbers in a Given an Array

The previous Ruby Coding Challenge was about counting how many items are prime in a given array.

This one is quite similar and as usual, we’ll:

  • Solve the problem manually.
  • Solve the problem in a more lovable Ruby way.

Let’s get into it

#1 - Let’s Revisit the Previous Ruby Coding Challenge

Below, we have the final version of our algorithm in Ruby, which has the method count_prime_numbers()

Ruby
 




xxxxxxxxxx
1
19


 
1
def count_prime_numbers(array)
2
  array.count do |item|
3
    is_prime(item)
4
  end
5
end
6
 
          
7
def is_prime(item)
8
  return false if item == 1
9
 
          
10
  (2..(item - 1)).each do |number|
11
    if item % number == 0
12
      return false
13
    end
14
  end
15
  return true
16
end
17
 
          
18
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
19
puts count_prime_numbers(array)



#2 - Creating the Sum Method

Let’s replace the count_prime_numbers() method by a method that sum the prime numbers, which will be creatively called sum_prime_numbers():

The new method should:

  • Iterate over the array.
  • Check if the current item is prime.
  • Sum the prime item.

Pretty simple. Translating into Ruby code:

Ruby
 




xxxxxxxxxx
1
12


 
1
def sum_prime_number(array)
2
  sum = 0
3
  for item in array
4
    if is_prime_number(item)
5
      sum += item
6
    end
7
  end
8
  return sum
9
end
10
 
          
11
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
12
puts sum_prime_number(array)



Cool. But what about the lovable Ruby way to solve problems?

#3 - Ruby Way

One important Ruby style is that we can manipulate arrays without worrying about temporary variables. Nice!

Going in parts, we can at least two favors to Ruby:

  • Hey Ruby, given an array, select only items that match a specific condition
Ruby
 




xxxxxxxxxx
1


 
1
array.select { |item| is_prime(item) }



Beautiful. Almost plain English.

If you execute the code above, you’ll end up with a smaller array, with only prime numbers:

Ruby
 




xxxxxxxxxx
1


 
1
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2
puts array.select { |item| is_prime(item) }
3
# result: [2, 3, 5, 7]



The next step is asking Ruby another favor:

  • Hey Ruby, by the way, now that you have an array with only prime numbers, reduce this array into a sum of all of the items
Ruby
 




xxxxxxxxxx
1


 
1
array.select{ |item| is_prime(item) }.reduce(:+)



Beautifully, Ruby has the method reduce(), which receives the + method that will be used to sum all of the items

The complete code:

Ruby
 




xxxxxxxxxx
1
17


 
1
def sum_prime_number(array)
2
    array.select{ |item| is_prime(item) }.reduce(:+)
3
end
4
 
          
5
def is_prime(item)
6
  return false if item == 1
7
 
          
8
  (2..(item - 1)).each do |number|
9
    if item % number == 0
10
      return false
11
    end
12
  end
13
  return true
14
end
15
 
          
16
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
17
puts sum_prime_number(array)



Notice that we ended up having a more functional style since this code is more declarative. I’m preparing a series to explore Functional Programming in JavaScript, Python, Java, and Ruby with some comparisons with Haskell. I hope to get you into this journey :)

I hope that you’ve enjoyed 1 thing or maybe 2 and see you in the next coding challenge :)


Don’t forget to keep in touch and say hi Alex :)

Twitter Youtube Instagram Linkedin

Topics:
algorithm, algorithm analysis, code, coding, functional programing, programming, rails, 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 }}