aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/abigail/node/ch-1.js
blob: e44318f34ced7240491698bf44ef03f83042da20 (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
30
#!/usr/local/bin/node

//
// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003
//

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

let ugly   = [1]
let next_2 =  0
let next_3 =  0
let next_5 =  0

  require ('readline')
. createInterface ({input: process . stdin})   
. on              ('line', n => {
    n =+ n
    while (ugly . length < n) {
        ugly . push (Math . min (2 * ugly [next_2], 
                                 3 * ugly [next_3],
                                 5 * ugly [next_5]))

        if (2 * ugly [next_2] <= ugly [ugly . length - 1]) {next_2 ++}
        if (3 * ugly [next_3] <= ugly [ugly . length - 1]) {next_3 ++}
        if (5 * ugly [next_5] <= ugly [ugly . length - 1]) {next_5 ++}
    }
    console . log (ugly [n - 1])
})