-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from animanmaster/hacktoberfest/ruby-one-liners
Fibonacci Sequence - Ruby One-Liners
- Loading branch information
Showing
3 changed files
with
18 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,6 +239,10 @@ Place: Varanasi, India | |
Coding Experience:Python, C, CPP | ||
Email: [email protected] | ||
|
||
Name: [Malik Ahmed](https://github.com/animanmaster)<br /> | ||
Place: New Jersey | ||
Coding Experience: Ruby, Java, Python, C, C++, C#, Javascript | ||
|
||
Name: [Ian Emnace](https://github.com/igemnace) <br/> | ||
Place: Quezon City, Philippines <br/> | ||
Coding Experience: Software development in Javascript, in browser and with Node.js. Projects in React/React Native, Vue. Toolbox primarily includes Vim scripts, Unix shell, and miscellaneous Python/Perl/Ruby programs. <br/> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env ruby | ||
# Ruby version 2.5.1 | ||
fibonacci = ->(n) { n < 2 ? n : fibonacci.(n - 1) + fibonacci.(n - 2) } | ||
|
||
# Test this by running `ruby fibonacci.rb n` where n is the index in the fibonacci sequence to calculate. | ||
puts fibonacci.call(ARGV[0].to_i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env ruby | ||
# Ruby version 2.5.1 | ||
# Calculate the fibonacci sequence iteratively by passing along fib(n-2) and fib(n-1) through each iteration of the loop. | ||
# The resulting fibonacci number is the sum of the last iteration of the loop. | ||
fibonacci = ->(n) { n < 2 ? n : 1.upto(n - 2).inject([0, 1]) { |last_two, i| fib = last_two.sum and [last_two[1], fib] }.sum } | ||
|
||
# Test this by running `ruby fibonacci_iterative.rb n` where n is the index in the fibonacci sequence to calculate. | ||
puts fibonacci.call(ARGV[0].to_i) |