aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/abigail/awk/ch-2.awk
blob: 48fc14d33298ef03f25ee329ee450d1d5cd011d9 (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
#!/usr/bin/awk

#
# See ../README.md
#

#
# Run as: awk -f ch-2.awk < input-file
#


function count (target, this_fib, prev_fib, key) {
    if (!this_fib) {this_fib = 1}
    if (!prev_fib) {prev_fib = 1}
    key = target ";" this_fib
    if (!(key in cache)) {
        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]
}

{
    print count($1)
}