Euler Problem 8

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);
Advertisements

One thought on “Euler Problem 8

  1. Pingback: Project Euler problem 8 | code and effect

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s