Euler 004 ruby Solution

Largest palindrome product

Problem

https://projecteuler.net/problem=4

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.

Answer: 906609

Solution

euler004.rb

#!/usr/bin/env ruby
def palindrome_test(test_me)
  reversed = 0
  original = test_me

  while 0 < original
    reversed = reversed * 10 + (original % 10)
    original /= 10
  end

  (test_me == reversed)
end

def prob004_brute_force
  max_pali = 0
  (100..999).each do |i|
    (100..999).each do |j|
      t = i * j
      max_pali = t if (t > max_pali) && palindrome_test(t)
    end
  end
  max_pali
end

puts prob004_brute_force if __FILE__ == $PROGRAM_NAME

See Also

# cpp go java php ruby rust javascript
1
2
3  
4    
5    
6      
7          
8          
9          
10          
11          
12          
# cpp ruby
13
14
15
16
17
18
19
20  
21  
22  
23  
24