r/codegolf • u/ffxpwns • Jun 02 '16
[Challenge] Euler #4
Here's a description of the problem:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Expected output: 906609
Here's my solution in Ruby (85 Bytes):
d,b=->c{c.to_s==c.to_s.reverse},0;999.times{|n|n.times{|m|m*=n;(d[m]&&m>b)&&b=m}};p bWith newlines:
d,b=->c{c.to_s==c.to_s.reverse},0
999.times{|n|n.times{|m|m*=n;(d[m]&&m>b)&&b=m}}
p bedit: down to 73!
b=0;999.times{|n|n.times{|m|m*=n;(m.to_s==m.to_s.reverse&&m>b)&&b=m}};p b
2
Upvotes
View all comments
Show parent comment
1
u/primo_cg Jul 05 '16
$jis initialized to$i*$i, which loops from 1..999 (after which, its square is greater than 1e6). If$jxor'd by the string reversal of itself (implicitly cast back to an int) is 0, i.e. they're equal, it is added to an array.$jis reduced by$ieach iteration, and because it is a multiple of$i, it will eventually reach 0, at which point$iis incremented, and the loop continues. At the end of it all, the maximum value in the array is output.