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

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

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

ARGF . each_line do
    |n|
    n = n . to_i
    while ugly . length < n do
        ugly . push ([2 * ugly [next_2],
                      3 * ugly [next_3],
                      5 * ugly [next_5]] . min)

        next_2 += 1 if 2 * ugly [next_2] <= ugly [-1]
        next_3 += 1 if 3 * ugly [next_3] <= ugly [-1]
        next_5 += 1 if 5 * ugly [next_5] <= ugly [-1]
    end
    puts (ugly [n - 1])
end