aboutsummaryrefslogtreecommitdiff
path: root/challenge-148/abigail/ruby/ch-2.rb
blob: 860f1ff1b767896d6819a0fd67198f35e2ca4981 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/ruby

#
# See https://theweeklychallenge.org/blog/perl-weekly-challenge-148
#
 
#
# Run as: ruby ch-2.rb
#

count = 5
out   = []
a_i   = 0
b_i   = 1
c_i   = 2
sum_i = 3
for i in 0 .. count - 1 do
    out . push ([999999, 999999, 999999, 999999 * 3])
end

max_index = 0
k         = 0

while 3 * k + 2 < out [max_index] [sum_i] do
    a  = 3 * k + 2
    f1 =     k + 1
    f2 = 8 * k + 5

    d1 = []
    for i in 1 .. f1 do
        if i * i > f1 then
            next
        end
        if f1 % i == 0 then
            d1 . push (i)
            if i != f1 / i then
                d1 . push (f1 / i)
            end
        end
    end

    d2 = []
    for i in 1 .. f2 do
        if i * i > f2 then
            next
        end
        if f2 % i == 0 then
            s1 = Math . sqrt(i)      . floor()
            s2 = Math . sqrt(f2 / i) . floor()
            if s1 * s1 == i then
                d2 . push (s1)
            end
            if (s2 * s2 == f2 / i) && s1 != s2 then
                d2 . push (s2)
            end
        end
    end

    d1 . each do |d1v|
        d2 . each do |d2v|
            b = d1v * d2v
            c = f1 * f1 * f2 / (b * b)
            if a + b + c < out [max_index] [sum_i] then
                seen = false
                out . each do |entry|
                    if entry [a_i] == a && entry [b_i] == b then
                        seen = true
                    end
                end
                if seen then
                    next
                end

                out [max_index] = [a, b, c, a + b + c]

                max_index = 0
                max_sum   = out [max_index] [sum_i]
                for i in 1 .. count - 1 do
                    if max_sum < out [i] [sum_i] then
                        max_index = i
                        max_sum   = out [i] [sum_i]
                    end
                end
            end
        end
    end
    k = k + 1
    if k > 10 then
        break
    end
end

out . each do |entry|
    printf "%d %d %d\n", entry [a_i], entry [b_i], entry [c_i]
end