Wednesday, February 27, 2008

Project Euler

A friend of mine pointed me to Project Euler. Being a mathematician and a programmer, he knew I would love it. It is a lot of fun. It reminds me of these computer competitions I used to participate in when I was in high school. You would get some list of problems on sheet of paper, have to code a solution, and a judge would come by to test your program. Anyways, I am a 4% genius having completed seven problems so far. I am mikeg on the message boards on there, if you want to check out my solutions. So far I have done everything in Ruby. I picked Ruby for two reasons. First, I like its syntax and want to use it more often. Second, it is slow. So I figured it would force me to come up with good algorithms. Here is a sample of one of my solutions. The problem was to find the largest palindrome that is the product of two 3-digit integers.

def isPal(num)
if num % 11 == 0
return num.to_s == num.to_s.reverse
end
return false

end
 
max = 0
999.downto(100){ |i|
i.downto(100){|j|
val = i*j
if val > max
if isPal(val)
max = val
end
else
break
end
}
}
puts max