Largest palindrome product
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.
https://projecteuler.net/problem=4
Digits Array
First let’s us create a function that splits a number into digits. There are several ways to do this.
Converting to a string, splitting, and parsing back to int.
Using mod and division to lop off the 1’s digit.
Palindromic
To check if a number is palindromic, we will copy the digits array and reverse the array and check if the values are still equal. Instead of iterating, each element to check for equality we will use a hack and join the array into a string.
Another way to check if a number is palindromic is to reverse all the digits and see if they are the same. Let’s give that way a shot.
Much faster! We will use this version for our brute force solution.
Domain
We want to find the largest palindrome made from the product of two 3-digit numbers. The smallest 3-digit number is 100. The largest 3-digit number is 999.
This means that the number we are looking for is the range of $[100\times100\,,999\times999]$, or $[10,000\,,998,001]$.
We can find this number by brute force by multiplying each number between 100 and 999 with every number between 100 and 999.
Instead of having two nested for loops going from min to max, the inner loop can be set to start at the current outer loop value. This will prevent us from calculating those values twice. For example: when the outer loop is at 101, if the inner loop started at 100, that value would have already been calculated when the outer loop was at 100 and the inner loop was at 101.