aboutsummaryrefslogtreecommitdiff
path: root/challenge-125/abigail/awk/ch-1.awk
blob: 1758b7d64d2c29a3c68bf18f1c2e5345108d03e1 (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
#!/usr/bin/awk

#
# See ../README.md
#

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

function introot (square) {
    return (int (.4 + sqrt (square)))
}


$1 <= 2 {
    print (-1)
    next
}


{
    n    = $1
    n_sq = n * n
    c    = n + 1
    c_sq = n_sq + 2 * n + 1
    while (2 * c - 1 <= n_sq) {
        b_sq = c_sq - n_sq
        b    = introot(b_sq)
        if (b_sq == b * b) {
            print n, b, c
        }
        c_sq += 2 * c ++ + 1
    }

    max_a = int (n / sqrt (2))
    for (a = 3; a <= max_a; a ++) {
        b_sq = n_sq - a * a
        b    = introot(b_sq)
        if (b_sq == b * b) {
            print a, b, n
        }
    }
}