-2

I have been working on this for a while, and I can't quite figure out why my code is printing 2091059712, which is apparently incorrect. For those who don't know, the challenge is to find the 13 adjacent digits in that long string that multiply to equal the highest number product.

package Project_Euler;

public class problem8 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //These 2 variables are chosen by the user. For the purpose of Euler Challenge, I have these values set as they are.
        int numToTest = 13;
        String num = "731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949" +
            "49545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111" +
            "21722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121" +
            "88399879790879227492190169972088809377665727333001053367881220235421809751254540594752243525849077116705560136048395864467" +
            "06324415722155397536978179778461740649551492908625693219784686224828397224137565705605749026140797296865241453510047482166" +
            "37048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456" +
            "82848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918" +
            "88458015616609791913387549920052406368991256071760605886116467109405077541002256983155200055935729725716362695618826704282" +
            "52483600823257530420752963450";

        //setting basic variables
        int test = numToTest - 1;
        int length = num.length();
        int[] array = new int[length];
        int result = 0;
        int[] testing = new int[numToTest];
        int product = 1;

        //Setting the array to have values equal to the corresponding values in the number above.
        for(int n = 0; n <= length - 1; n++){
            //The minus 48 is because the code adds 48 to the value for some reason
            array[n] = Integer.valueOf(num.charAt(n) - 48);
        }

        //Multiplies 13 digits together and tests which set comes out with the highest product
        for(int n = 0; n < length; n++){

            //assures that it only checks when there are enough digits to multiply together
            if(n + test < length){
                product = 1;

                //goes through the digits, multiplying them by the next one.
                for(int check = 0; check <= test; check++){
                        product *= array[n + check];
                        testing[check] = array[n + check];
                }
                //finds the greatest product
                if(product > result){
                    result = product;
                }
            }
        }
        //Prints the greatest product
        System.out.println(result);
    }

}

I have checked that the numbers involved are correct, and same with each code statement.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118

2 Answers2

1

The ints result and product are too big to fit into a 32-bit integer, choose to use long or BigInteger (64-bits and more respectively.)

You are performing integer arithmetic where you multiply 13 together; such a product would cause overflow to int, where the max value is about 2 billion

EDToaster
  • 3,160
  • 3
  • 16
  • 25
0
private static long productSeries() {
        final String num = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";



    long greatestProduct = 0;
    int adjacentDigit = 13;

    for (int i = 0; i < num.length() - adjacentDigit; i++) {
        long sum = 1;

        for (int j = 1; j <= adjacentDigit; j++) {
            sum *= (long) Character.getNumericValue(num.charAt(i + j));
        }

        if (sum > greatestProduct) {
            greatestProduct = sum;
        }
    }
    return greatestProduct;
}

public static void main(String[] args) {
    long result = productSeries();
    System.out.print(result);
}
kheengz
  • 840
  • 12
  • 10