We have started learning Java in school, and we have been given a few homeworks to do. I’ve managed to do 4 out of 5, but this last one is a real pain.
Basicly: Write a program that finds (in a 1000 places long number) the largest product of five
consecutive digits.
Here’s the number http://pastebin.com/PFgL6jcM
Do you have any ideas how to solve this ?
If this are unclear instruction, notify me and will try to explain to you again.
I’d say an optimized algorithm would look like this:
1) grab first five numbers
2) if current set contains a
0, grab the first five numbers after the0. Do this until you reach a set that doesn’t contain a0. (if all sets contain a0– unlikely – return0).3) compute the product of the 5 numbers (
x1,x2,x3,x4,x5) like so:4) if
pis greater than the previousp, store it.5) discard the first number and add the next one (
x6).6) if the new
pis greater than the old one, go to step 3)You’re reducing the number of multiplications by a factor of 5, since you won’t keep multiplying 5 numbers, but 2.
Remember to discard sequences that contain a
0and try to optimize the algorithm along these lines.