🍻


Project-Euler / 8

Problem 8 asks:

Find the greatest product of five consecutive digits in the 1000-digit number.

7316717653133062491922511[…]3600823257530420752963450

I did a simple bruteforce in Java:

import java.nio.file.*;
import java.io.IOException;

public class Problem8 {
    public static void main(String[] args) {
        Path path = Paths.get("problem8.txt");
        try {
            byte[] bigNum = Files.readAllBytes(path);
            int result = 0;
            for (int i=0; i<bigNum.length-5; i++) {
                int product = 1;
                for (int j=i; j<i+5; j++) product *= bigNum[j] - '0';
                if (product > result) result = product;
            }
            System.out.println(result);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

No optimizations and runs in under 0.14 seconds (on a 2.26GHz CPU).

Last updated on .