DEV Community

Discussion on: Project Euler Problem 4 Solved with Javascript

dhurak profile image
Dhurak • Edited

for (let j = largestNum; j > 0; j--)

can become:

for (let j = i; j > 0; j--)

The inner loop does not have to start from the very beginning every time.
First set would be 99x99, 99x98, 99x97... 99x1
Second set would be 98x98, 98x97, 98x96... 98x1
Third set: 97x97, 97x96, 97x95... 97x1

In second set you don't test 98x99 because you already tested 99x98 in first set.

Thread Thread
codenutt profile image
Jared Author

Ohhh, I see. That's genius. Thank you!