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
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Reactive Programming
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • File Upload Security and Malware Protection

Trending

  • Reactive Programming
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • File Upload Security and Malware Protection
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Day 7 of 30 Ruby Coding Challenge: Factorial Numbers

Day 7 of 30 Ruby Coding Challenge: Factorial Numbers

Day 7 of 30. We're going to explore how to calculate Factorial Numbers in Ruby. This will be a better version that can be still improved later on.

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

Join the DZone community and get the full member experience.

Join For Free

Hey folks!

In today’s post, which is the written version of the Youtube video, we’ll rewrite the previous algorithm with a little bit better version. It’ll be really quick, I promise.

#1 — Algorithm Version 1...As Usual, Ugly

Just to remember, this was the first algorithm version.

Ruby
 




xxxxxxxxxx
1
10


 
1
def factorial(number)
2
  result = number
3
  while number > 1
4
    result = result * (number — 1)
5
    number = number — 1
6
  end
7
  return result
8
end
9

          
10
puts factorial(1)



#2 — Algorithm Version 2. A Little Better

I’d like to keep the argument number intact, which means that I don’t want to use it to control my while loop.

Ruby has an amazing and easy way to deal with arrays in this case:

Ruby
 




xxxxxxxxxx
1


 
1
(1..(number — 1))



With the code above, we’re saying to Ruby to iterate with numbers between 1 and number — 1.

Notice that -1 is the pattern used for each iteration.

Ruby
 




xxxxxxxxxx
1


 
1
(1..(number — 1)).each do |item|
2

          
3
end



Notice that the method, each, gives us the current number, which is the item, for each iteration

The final version would be:

Ruby
 




xxxxxxxxxx
1


 
1
def factorial_version_2(number)
2
  final_result = number
3
  (1..(number — 1)).each do |item|
4
    final_result = final_result * item
5
  end
6
  return final_result
7
end
8
9
puts factorial_version_2(5)



As you can see, we avoid the number mutation, which means that our code is less error-prone and easier to read. Cool!

In the next video, we’ll refactor this code a little bit more to reflect a more Ruby way to solve things. See you there :)

Don’t forget to keep in touch and say hi!

Twitter Youtube Instagram Linkedin GitHub

Coding (social sciences) Algorithm twitter POST (HTTP) GitHub Instagram Rewrite (programming)

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

Opinions expressed by DZone contributors are their own.

Trending

  • Reactive Programming
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • File Upload Security and Malware Protection

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: