aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/abigail/node/ch-1.js
blob: 4398a98d5aa9ca4458ca7c9dd22cd309fd083e08 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/local/bin/node

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

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

//
// Find the GCD, using Euclids algorithm
//    (https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations)
//
function gcd (a, b) {
    if (b >  a) {return gcd (b, a)}
    if (b == 0) {return a}
                 return gcd (b, a % b)
}

function is_power_of_n (number, n) {
    if (number <  1) {return false}
    if (number == 1) {return true}
    if (number %  n) {return false}
                      return is_power_of_n (number / n, n)
}

function is_power_of_2 (number) {
    return is_power_of_n (number, 2)
}

  require ('readline')
. createInterface ({input: process . stdin})   
. on              ('line', line => {
    let [m, n] = line . trim () . split (' ') . map (x => +x)
    if (n % 2 == 1 || m % 2 == 1) {
        console . log (0)
        return
    }
    let  r     = gcd (m, n)
    if (r > 1 && is_power_of_2 (r)) {
        console . log (1)
    }
    else {
        console . log (0)
    }
})