So continuing the dual solving of problems with Anthony Doherty, he decided to choose Problem 8 for this week.
Again, I didn’t think the following solution would be very quick or efficient, but decided to go with the coding principle of make it work, make it pretty, make it fast. Java though completed the task in approximately 0 seconds according to NetBeans, so I was able to skip the make it fast step for the time being.
Update: Whilst typing out this post a slightly more efficient way to do things would be to only read in the next number from the string and overwrite the oldest – some kind of LIFO queue or list. It wouldn’t be difficult to implement, but having been out of the office yesterday, think I need to divert my attention elsewhere for a while.
Solution:
int index = 5;
int max = 0;
for (int i = 0; i < inputNumber.length() - index; i++) {
String sub = inputNumber.substring(i, i + index);
int j = Integer.parseInt(sub.substring(0, 1));
int k = Integer.parseInt(sub.substring(1, 2));
int l = Integer.parseInt(sub.substring(2, 3));
int m = Integer.parseInt(sub.substring(3, 4));
int n = Integer.parseInt(sub.substring(4, 5));
int sum = j * k * l * m * n;
if (sum > max) {
max = sum;
}
}
System.out.println("Max = " + max);
[...] here’s Greg’s response, posted simultaneously today. Share this:EmailShareFacebookDigg This entry was posted in Euler [...]