fork download
  1. def fib(n)
  2. return case(n)
  3. when 1; 1
  4. when 0; 0
  5. else fib(n-1)+fib(n-2)
  6. end
  7. end
  8. n=4; sum=0; 1.upto(n) {|x| sum+=fib(x);puts "#{x} = #{fib(x)}"}
  9. puts "SUM = #{sum}"
Success #stdin #stdout 0s 4760KB
stdin
Standard input is empty
stdout
1 = 1
2 = 1
3 = 2
4 = 3
SUM = 7