microslayer
microslayer

Reputation: 155

Get Wrong Result - Java

I am trying to construct a program that will tell me the product of the 13 adjacent numbers with the highest product. However, it keeps giving me the wrong answer. What is wrong with this code?

import java.lang.Character; 

public class ProjectEuler8 { public static void main(String[] args) { 

  String num = "73167176531330624919225119674426574742355349194934"+
"9698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511"+
"1254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113"+
"6222989342338030813533627661428280644448664523874930358907296290491560440772390713810515859307960866"+
"7017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243"+
"5258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482"+
"8397224137565705605749026140797296865241453510047482166370484403199890008895243450658541227588666881"+
"1642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042"+
"2421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188"+
"8458015616609791913387549920052406368991256071760605886116467109405077541002256983155200055935729725"+
"71636269561882670428252483600823257530420752963450";

  int NUM_LENGTH = num.length(); 

  char[] number = new char[NUM_LENGTH];

  for (int i=0; i<NUM_LENGTH; i++) { number[i] = num.charAt(i); }

  int mult=0; 
  int highest=0; 

  for (int j=0; j<=987; j++) {
  int a = number[j]; a=Character.getNumericValue(a); 
  int b = number[j+1]; b=Character.getNumericValue(b); 
  int c = number[j+2]; c=Character.getNumericValue(c); 
  int d = number[j+3]; d=Character.getNumericValue(d);   


  int e = number[j+4]; e=Character.getNumericValue(e); 
  int f = number[j+5]; f=Character.getNumericValue(f); 
  int g = number[j+6]; g=Character.getNumericValue(g); 
  int h = number[j+7]; h=Character.getNumericValue(h); 
  int i = number[j+8]; i=Character.getNumericValue(i); 
  int k = number[j+9]; k=Character.getNumericValue(k);
  int l = number[j+10]; l=Character.getNumericValue(l);
  int m = number[j+11]; m=Character.getNumericValue(m);
  int n = number[j+12]; n=Character.getNumericValue(n);

  mult = a*b*c*d*e*f*g*h*i*k*l*m*n;  
  if (mult > highest)
    highest = mult; 
  System.out.println(highest);} 

  }}

Upvotes: 1

Views: 121

Answers (3)

sprinter
sprinter

Reputation: 27946

Could I suggest a different approach using Java 8 (assuming you have it)? This might make things a bit easier to diagnose by getting rid of all the variables for holding the adjacent values.

List<Integer> digitList(String numberText) {
    return numberText.chars().map(c -> Character.digit(c, 10))
        .collect(Collectors.toList);
}

long highestAdjacentProduct(List<Integer> inputs) {
    int adjacentLength = 13;
    return IntStream.range(0, input.size() - adjacentLength)
        .mapToLong(i -> inputs.subList(i, i + adjacentLength)
            .stream().reduce(1, (n1, n2) -> n1 * n2)))
        .max();
}

This is used with highestAdjacentProduct(digitList(inputString))

The really nice thing about using streams like this is you can trivially add peek methods to print out intermediate values to ensure the calculations are working as you expect.

Upvotes: 0

goodmayhem
goodmayhem

Reputation: 3394

The code looks fine to me. It will give you a list of loop-size 988. Although you are putting a check to print value of highest product in each loop. I don't know what you mean by 'wrong' answer, i wonder if you even singled out the answer out of all the integers outputted by this function. It will be the last one by the way.

Suggestions : Store the temporary value of highest in some dummy integer and then print it out outside the loop.

There is a lot of scope to re-factor the code namely you could get rid of logic to get char array and you can do without the computational over head of multiplication of the integers when any of 13 digits is 0.

Upvotes: 2

You have integer overflow; just the input numbers are far too large to fit in 31 bits. You need to use BigInteger instead.

Upvotes: 0

Related Questions