aboutsummaryrefslogtreecommitdiff
path: root/challenge-125/abigail/ruby/ch-1.rb
blob: 91518fc95ed4eb9cbdebfaf823f99dca7c424b53 (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
#!/usr/bin/ruby

#
# See ../README.md
#
 
#
# Run as: ruby ch-1.rb < input-file
#

def introot (square)
    return Math . sqrt(square) . floor()
end

ARGF . each_line do
    |n|
    n = n . to_i
    if (n <= 2) then
        puts (-1)
    end

    n_sq = n * n
    c    = n + 1
    c_sq = n_sq + 2 * n + 1
    while (2 * c - 1 <= n_sq) do
        b_sq = c_sq - n_sq
        b    = introot (b_sq)

        if (b_sq == b * b) then
            puts (n . to_s + " " + b . to_s + " " + c . to_s)
        end

        c_sq += 2 * c + 1
        c    += 1
    end

    max_a = (n / Math . sqrt(2)) . floor()
    for a in 3 .. max_a do
        b_sq = n_sq - a * a
        b    = introot (b_sq)
        if (b_sq == b * b) then
            puts (a . to_s + " " + b . to_s + " " + n . to_s)
        end
    end
end