# Euler Project #4: Largest Palindrome

- - on
- in Code -

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

var int1 = 999, int2 = 999, product = 0, largestProduct = 0 while int1 > 900 { int2 = 999 while int2 >= int1 { product = int1 * int2 // this is ridiculous, but it works: if (product > largestProduct) && (product == Int(String(Array(String(product).characters.reverse())))) { largestProduct = product } int2-- } int1-- } print(largestProduct)

This took a ridiculously long time. I originally had a separate function to convert the product integer to a string, then an array of characters, then all the way back to an integer. That took much longer than I expected, because Swift and (lack of) documentation. It ended up being more efficient to put it all into one function, even though the lack of separation of duties makes me cringe . I also knew what the final integers were, so I was able to modify the first while loop to run more quickly to test. If I hadn’t known, I would have run the first loop down to 1.