mirror of
https://github.com/c0de-archive/hacktoberfest-2018.git
synced 2024-12-22 05:12:40 +00:00
adding fibonacci.rb
a fibonacci code in ruby.
This commit is contained in:
parent
385b846eed
commit
da016b3828
21
code/fibonacci.rb
Normal file
21
code/fibonacci.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#####################
|
||||||
|
# Fibonacci in Ruby #
|
||||||
|
#####################
|
||||||
|
# main fibonacci function
|
||||||
|
def CalculateFibonacci(n)
|
||||||
|
if (n == 0)
|
||||||
|
return 0
|
||||||
|
elsif (n <= 2)
|
||||||
|
return 1
|
||||||
|
else
|
||||||
|
return CalculateFibonacci(n - 1) + CalculateFibonacci(n - 2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
# execution with check regarding the amount of args
|
||||||
|
if(ARGV.length < 1)
|
||||||
|
puts "argument needed. term number to iterate to, with 0 as the first term number"
|
||||||
|
else
|
||||||
|
for arg in ARGV
|
||||||
|
puts arg.to_s + ": " + CalculateFibonacci(arg).to_s
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user