DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • A Data-Driven Approach to Application Modernization
  • Auto-Scaling Kinesis Data Streams Applications on Kubernetes
  • Managing Data Residency, the Demo
  • What I Learned From Crawling 100+ Websites
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Day 4 of 30 - Ruby Coding Challenge - Prime Algorithm Recursively

Day 4 of 30 - Ruby Coding Challenge - Prime Algorithm Recursively

Day 4 of 30 - Ruby Coding Challenges in 30 Days. I want to rewrite the previous coding challenge using recursion, which was counting how many items are in a given array

Alexandre Gama user avatar by
Alexandre Gama
·
Jun. 16, 20 · Tutorial
Like (3)
Save
Tweet
Share
2.84K Views

Join the DZone community and get the full member experience.

Join For Free

Hey!

Day 4 of 30: Ruby Coding Challenge number 4. This is the post version of the Youtube video.

I want to rewrite the coding challenge number 3 using recursion, which was:

How many prime items are in a given array?

Let’s get into it.

#1 - First Algorithm Version

It was the final algorithm version:

Ruby
xxxxxxxxxx
1
19
 
1
def count_prime_number_version_2(array)
2
  array.count do |item|
3
    is_prime_number(item)
4
  end
5
end
6
7
def is_prime_number(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_number_recursively(array)


Instead of having that array loop in the is_prime_number() method:

Ruby
xxxxxxxxxx
1
10
 
1
def is_prime_number(item)
2
  return false if item == 1
3
4
  (2..(item - 1)).each do |number|
5
    if item % number == 0
6
      return false
7
    end
8
  end
9
  return true
10
end


We’re going to rewrite this method using recursion.

#1 Recursive Algorithms

I know. Recursion is one of the monsters when it comes to coding, along with memory management and giving names to variables.

Just a quick reminder:

A recursive algorithm calls itself with a smaller version of its own problem

I’m not going to go into much detail about recursion because I’m kind of assuming you already know something about it, however, I might be wrong. Because of that, I’m recording and writing a special tutorial to go through the benefits (and some costs) of recursive algorithms.

Step 1:

A recursive function calls itself

Ruby
xxxxxxxxxx
1
 
1
def is_prime_number(item)
2
  return is_prime_number(item)
3
end


Step 2:

We need at least one condition to call the function again, which is when item is not evenly dividable by number

Ruby
xxxxxxxxxx
1
 
1
def is_prime_number(item)
2
  return is_prime_number(item) if item % number != 0
3
end


Step 3:

We already know that 1 is not a prime number, as we’ve seen in the previous algorithm.

Ruby
xxxxxxxxxx
1
 
1
def is_prime_number(item)
2
    return false if item == 1
3
4
  return is_prime_number(item) if item % number != 0
5
end


Step 4:

We should call the method itself with the current number - 1, which is the logic we have using so far

Ruby
xxxxxxxxxx
1
 
1
def is_prime_number(item, number)
2
    return false if item == 1
3
4
  return is_prime_number(item, number - 1) if item % number != 0
5
end


Step 5:

We should return true if number reaches 1, which means that the number is actual prime

Ruby
xxxxxxxxxx
1
 
1
def is_prime_number(item, number)
2
    return false if item == 1
3
    return true if number == 1
4
  return is_prime_number(item, number - 1) if item % number != 0
5
end


The entire code is:

Ruby
x
14
 
1
def count_prime_numbers(array)
2
  array.count do |item|
3
    is_prime_number(item, item - 1)
4
  end
5
end
6
7
def is_prime_number(item, number)
8
    return false if item == 1
9
    return true if number == 1
10
  return is_prime_number(item, number - 1) if item % number != 0
11
end
12
13
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
14
puts count_prime_numbers(array)


And that’s it!

To be honest, this is not an easy topic (at least for me) to explain in writing and maybe you get a better picture watching the video :)

Just a reminder. This is not about Ruby; it’s all about concepts and logics and having some fun while solving these problems.

Thanks for your visit, let’s keep in touch and see you in the next challenge :)

Twitter Youtube Instagram Linkedin

Algorithm PRIME (PLC) Coding (social sciences)

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

Opinions expressed by DZone contributors are their own.

Trending

  • A Data-Driven Approach to Application Modernization
  • Auto-Scaling Kinesis Data Streams Applications on Kubernetes
  • Managing Data Residency, the Demo
  • What I Learned From Crawling 100+ Websites

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: