Monday, April 26, 2010

4 down

Finished problem #4, which required finding the largest six-digit palindrome number with three digit divisors. The interesting shortcuts for me on this one were that if a number has six digits, then the square root of that number will have three digits (since one million is 1000^2 and 100,000 is roughly 316^2. This made me realize that I could just check divisors starting at 999 and decrementing the potential divisor by one until I reached the square root. This dramatically cut down on the number of divisions to make each time.

It’s not a very generalizable program, unlike some of the others – finding largest prime factor of a number, find the sum of a certain number of values of a sequence – which could be used for other parameters by changing the values of some of the initial conditions. The solution program for this problem was really specifically designed to solve this problem, and it’s not really useful for other (5 digit, 7 digit, etc.) numbers. The structure is basically the same, but it would require more rewriting of the function that generates the palindrome numbers.

A more general solution would involve generating palindromes between the values of x and y and then finding the largest one that is a perfect square or something like that.

Looking at the answers, I was actually a little surprised how much I overthought it. It turns out you can just brute force all the products of 3-digit numbers and then check to see if its a palindrome and you get an answer that way, too. I'm already starting to think about the balance between code that is easy to read (i.e., a simple algorithm) versus code the runs quickly.

No comments:

Post a Comment