Wednesday, November 9, 2011

Euler Problem 145

My try to solve project euler problem no. 145 using Ruby

#max = 10**9
max = 89990001 #no other result beyond this value, try for yourself :)

total_reversible = 0

(1..max).each do |my_num_a|
  if my_num_a % 2 != 0
    my_result = (my_num_a + my_num_a.to_s.reverse.to_i).to_s
    my_scan = my_result.gsub(/[02468]/,'*')
    if my_scan === my_result
      #puts my_num_a
      total_reversible += 1
    end
  end
end
puts total_reversible * 2 # why multiply by two? because each value and its reversible value are accounted

No comments:

Post a Comment