Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fibonacci Sequence - Ruby One-Liners #225

Merged

Conversation

animanmaster
Copy link
Contributor

@animanmaster animanmaster commented Oct 30, 2018

Please describe your program and how to run it.

This PR adds two Ruby scripts to print out the n-th number of the Fibonacci Sequence. (Related to Issue #210 for #hacktoberfest)

fibonacci.rb implements this recursively, clearly following the mathematical definition of the Fibonacci sequence. However, as you all know, this naive implementation can be highly inefficient, and should not be executed for large n values unless you enjoy hearing your computer's fan whir. :)

fibonacci_iterative.rb implements this iteratively, keeping track of the last two Fibonacci numbers each iteration until we reach the final result.

Both programs can be run while passing an n value into the script, like so:

λ ruby fibonacci.rb 10
55

λ ruby fibonacci_iterative.rb 10
55

But as mentioned above, fibonacci_iterative is the more efficient version. For comparison, take a look at the runtime of both versions when n = 30:

$ time ruby fibonacci.rb 30
832040

real    0m0.554s
user    0m0.266s
sys     0m0.109s

$ time ruby fibonacci_iterative.rb 30
832040

real    0m0.088s
user    0m0.063s
sys     0m0.016s
λ powershell -Command "Measure-Command {ruby fibonacci.rb 30}"


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 375
Ticks             : 3753132
TotalDays         : 4.34390277777778E-06
TotalHours        : 0.000104253666666667
TotalMinutes      : 0.00625522
TotalSeconds      : 0.3753132
TotalMilliseconds : 375.3132

λ powershell -Command "Measure-Command {ruby fibonacci_iterative.rb 30}"


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 133
Ticks             : 1336232
TotalDays         : 1.54656481481481E-06
TotalHours        : 3.71175555555556E-05
TotalMinutes      : 0.00222705333333333
TotalSeconds      : 0.1336232
TotalMilliseconds : 133.6232

What Programming Language?

Ruby

@wzhouwzhou wzhouwzhou added hacktoberfest-accepted Hacktoberfest! Lang: Ruby Contributions made in Ruby labels Oct 30, 2018
@wzhouwzhou
Copy link
Owner

Thank you for making the first Ruby program in this repository, as well as for comprehensive testing!

@wzhouwzhou wzhouwzhou added the Lang: First Contributor First program of a certain language in this repository label Oct 30, 2018
@wzhouwzhou wzhouwzhou merged commit 9791142 into wzhouwzhou:master Oct 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted Hacktoberfest! Lang: First Contributor First program of a certain language in this repository Lang: Ruby Contributions made in Ruby
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants