Skip to content

Commit

Permalink
Merge pull request #225 from animanmaster/hacktoberfest/ruby-one-liners
Browse files Browse the repository at this point in the history
Fibonacci Sequence - Ruby One-Liners
  • Loading branch information
wzhouwzhou authored Oct 30, 2018
2 parents 8d44dde + bc73b5e commit 9791142
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/>
Expand Down
6 changes: 6 additions & 0 deletions ruby/fibonacci.rb
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)
8 changes: 8 additions & 0 deletions ruby/fibonacci_iterative.rb
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)

0 comments on commit 9791142

Please sign in to comment.