### plus from min to max ###
2.0.0-p598 :018 > def fib(min,max)
2.0.0-p598 :019?> a=min.upto(max).inject(:+)
2.0.0-p598 :020?> a
2.0.0-p598 :021?> end
=> nil
2.0.0-p598 :022 > fib(1,4)
=> 10
2.0.0-p598 :023 > fib(-1,4)
=> 9
### multiple from min to max###
2.0.0-p598 :028 > def fact(min,max)
2.0.0-p598 :029?> a = min.upto(max).inject(:*)
2.0.0-p598 :030?> a
2.0.0-p598 :031?> end
=> nil
2.0.0-p598 :032 > fib(1,4)
=> 10
2.0.0-p598 :033 > fact(1,4)
=> 24
Very simple....
No comments:
Post a Comment