Aug-06-2020, 10:48 PM
What a fun programming challenge. It is fun just coming up with a solution, but there are many different optimizations that can be tried adding additional challenge. I went from trying 810,000 combinations to 52,115 combinations to 6207 combinations to find the solution.
I think you should restart from scratch and the first thing you solve is to write a test that determines if a number is a palindrome. I wrote mine as a function. That way I could easily try different algorithms and measure how it affected execution time.
Once you get the palindrome test working you need to design an algorithm that tests all possible solutions and reports which combination of numbers produced the highest value palindrome. I am using a for loop. I know the range of three digit numbers so it is easy to specify the range of numbers over which I want to iterate. I don't think it makes much sense converting a number to a string just to count the number of digits. After I got a brute force solution that tests 810,000 combinations of numbers I found some optimizations that eventually reduced that to 6207. The big performance leaps came from analyzing the problem and reducing the range of number I needed to test.
I think you should restart from scratch and the first thing you solve is to write a test that determines if a number is a palindrome. I wrote mine as a function. That way I could easily try different algorithms and measure how it affected execution time.
Once you get the palindrome test working you need to design an algorithm that tests all possible solutions and reports which combination of numbers produced the highest value palindrome. I am using a for loop. I know the range of three digit numbers so it is easy to specify the range of numbers over which I want to iterate. I don't think it makes much sense converting a number to a string just to count the number of digits. After I got a brute force solution that tests 810,000 combinations of numbers I found some optimizations that eventually reduced that to 6207. The big performance leaps came from analyzing the problem and reducing the range of number I needed to test.