aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/abigail/ruby/ch-2.rb
blob: 630b4e3dd052a3a458a491f070d4ccfdc11e2193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/ruby

#
# See ../README.md
#
 
#
# Run as: ruby ch-2.rb < input-file
#

$cache = {}

def _count (target, this_fib, prev_fib)
    key = target . to_s + ";" + this_fib . to_s
    $cache [key] ||= target <  this_fib ? 0
                   : target == this_fib ? 1
                   : _count(target - this_fib, this_fib + prev_fib, this_fib) +
                     _count(target,            this_fib + prev_fib, this_fib)
    return $cache [key]
end

def count (target)
    return _count(target, 1, 1)
end

ARGF . each_line do
    | line |
    puts (count(line . to_i))
end