Done puzzle 30
[project-euler.git] / euler-4.rb
1
2 def is_palindrome?(n)
3 n.to_s == n.to_s.reverse
4 end
5
6
7 a = b = limit = 999
8
9 directions = [:left, :back, :down, :forward]
10
11 direction = :left
12
13 puts "#{a}/#{b}, product = #{a*b}, dir = #{direction}"
14
15 while not is_palindrome?(a * b) do
16 case direction
17 when :left
18 a -= 1
19 direction = :back
20 when :down
21 b -= 1
22 direction = :forward
23 when :back
24 if a < limit
25 a += 1
26 else
27 direction = :forward
28 end
29 b -= 1
30 when :forward
31 if b < limit
32 b += 1
33 else
34 direction = :back
35 end
36 a -= 1
37 end
38 # puts "#{a}/#{b}, product = #{a*b}, dir = #{direction}"
39 end
40
41 if __FILE__==$0
42 puts "Largest palindrome is #{a*b}, the product of #{a} and #{b}"
43 end
44
45
46
47
48
49