aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/abigail/node/ch-2.js
blob: f74073f55524110e59d512aedbdf56bb70613e5e (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/local/bin/node

//
// See ../README.md
//

//
// Run as: node ch-2.js < input-file
//

let cache = {}

function count (target, this_fib, prev_fib) {
    if (!this_fib) {this_fib = 1}
    if (!prev_fib) {prev_fib = 1}
    let 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]
}


  require ('readline')
. createInterface ({input: process . stdin})   
. on              ('line', line => console . log (count (+line)))