Question
Is there a better way to find the solution of the Project Euler problem 8, which is Find the greatest product of five consecutive digits in the 1000-digit number, than the brute force method.
I calculated all possible products and selected the greatest one — brute force algorithm.
Is there a more efficient algorithm? Or is the brute force method the only way.
Side notes
- This is not a homework question.
- I am not asking for the result to problem 8.
Since the question asks for consecutive digits, ‘brute force’ means O(n) in this case, n being the number of digits (1000). As long as there is not some sort of pattern in the number, you will need n steps for just scanning the number, so this is the fastest solution.
You can cache the product of the last 4 digits or do similar tricks, but you definitely won’t get better than O(n).