"Problem 8
Find the greatest product of five consecutive digits in the 1000-digit number."
Well, this problem can be solved even without computer.
Just use the best tool that you never had: your brain ;)
Use the "Find" command in your favorite text editor to highlight all 9 in the given 1000-digit number.
Is it not easy to find a combination of 99879?
But if you wanna code:
JavaScript (Spidermonkey)
var initial = [the given 1000-digit number as array of integers]; var answer = 0; var sum = 0; var bestsum = 0; var tarr = [5]; for(var c = initial.length - 1; c --> 3;) { sum = 0; for(var s = c; s > c-5; --s) sum += initial[s]; if(sum > bestsum) { bestsum = sum; for(var j = c, i = 4; j > c-5; --j, --i) tarr[i] = initial[j]; } } answer = tarr[0] * tarr[1] * tarr[2] * tarr[3] * tarr[4]; print(answer);
No comments:
Post a Comment