diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 795872f3..271b5c13 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -239,6 +239,10 @@ Place: Varanasi, India Coding Experience:Python, C, CPP Email: sankur.shrikant.eee16@itbhu.ac.in +Name: [Malik Ahmed](https://github.com/animanmaster)
+Place: New Jersey +Coding Experience: Ruby, Java, Python, C, C++, C#, Javascript + Name: [Ian Emnace](https://github.com/igemnace)
Place: Quezon City, Philippines
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.
diff --git a/ruby/fibonacci.rb b/ruby/fibonacci.rb new file mode 100644 index 00000000..a8e5ba37 --- /dev/null +++ b/ruby/fibonacci.rb @@ -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) diff --git a/ruby/fibonacci_iterative.rb b/ruby/fibonacci_iterative.rb new file mode 100644 index 00000000..37127f7d --- /dev/null +++ b/ruby/fibonacci_iterative.rb @@ -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)