2

I know my code can be simpler and more efficient... My code is supposed to grab the biggest set of 5 digits. It works, except it only is grabbing 3 digits, what would i need to modify to change that?

public class thousandDigits {
    public static void main(String[] args) {
        int greatest = 0;
        String num = ("73167176531330624919225119674426574742355349194934"
                + "96983520312774506326239578318016984801869478851843"
                + "85861560789112949495459501737958331952853208805511"
                + "12540698747158523863050715693290963295227443043557"
                + "66896648950445244523161731856403098711121722383113"
                + "62229893423380308135336276614282806444486645238749"
                + "30358907296290491560440772390713810515859307960866"
                + "70172427121883998797908792274921901699720888093776"
                + "65727333001053367881220235421809751254540594752243"
                + "52584907711670556013604839586446706324415722155397"
                + "53697817977846174064955149290862569321978468622482"
                + "83972241375657056057490261407972968652414535100474"
                + "82166370484403199890008895243450658541227588666881"
                + "16427171479924442928230863465674813919123162824586"
                + "17866458359124566529476545682848912883142607690042"
                + "24219022671055626321111109370544217506941658960408"
                + "07198403850962455444362981230987879927244284909188"
                + "84580156166097919133875499200524063689912560717606"
                + "05886116467109405077541002256983155200055935729725"
                + "71636269561882670428252483600823257530420752963450");

        for (int n = 0; n < num.length() - 5; n++) {
            greatest = ((num.charAt(n)) + (num.charAt(n+1)) + (num.charAt(n+2)) + (num.charAt(n+3))
                    + (num.charAt(n+4)));
            if (greatest > n) {
                n = greatest;
            }
        }
        System.out.print(greatest);
    }
}

OUTPUT:

357
Frank
  • 137
  • 9
  • 1
    Your code doesn't work. There is 3 digit number 963 which is greater than 357. – V__ Nov 19 '15 at 05:41
  • 1
    I think you mean `num.charAt(n+1)` not `(num.charAt(n)+1)`. Also, you need to multiply the first digit by 10^(6-n) where n=1 to start with for each digit n=1...5 – deepmindz Nov 19 '15 at 05:42

4 Answers4

3

I think you want to use String.substring(int, int) to iterate all possible 5 character substrings, and then you might use Math.max(int, int) to update greatest. Something like

int greatest = Integer.MIN_VALUE;
for (int i = 0; i < num.length() - 4; i++) {
    // int value = Integer.parseInt(num.substring(i, i + 5));
    int value = Integer.parseInt(String.valueOf(num.charAt(i))
                + num.charAt(1 + i) + num.charAt(2 + i) + num.charAt(3 + i)
                + num.charAt(4 + i));
    greatest = Math.max(greatest, value);
}
System.out.println(greatest);

I get 99890.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
3

I think you are trying to add 5 consecutive characters to get sum, and store starting index of highest sum.

But you should be using Character.getNumricValue(char) to convert (num.charAt(n)) to numeric value and then add.

greatest  = Character.getNumericValue((num.charAt(n)) + Character.getNumericValue((num.charAt(n+1)) + Character.getNumericValue((num.charAt(n+2)) + 
Character.getNumericValue((num.charAt(n+3)) + 
Character.getNumericValue((num.charAt(n+4));

You need a valirable to store old value to compare and index

if(greatest > oldGreatest) {
    index = n;
} 

Then finally print using index out side loop:

System.out.print((num.charAt(index)) + (num.charAt(index+1) + (num.charAt(index +2)) + (num.charAt(index +3)) + (num.charAt(index +)));
jbhardwaj
  • 379
  • 3
  • 8
2

I think you'll find it's not grabbing three digits, but rather the sum of the six characters you are pulling out is a 3-digit number.

If you're after the largest five digit number, you need to extract five digits (not six) as you do and assign them a weight. So the first digit must be multiplied by 10,000, the second by 1,000 and so on.

But there's more: you're are getting the character at an index within your string. This is not what you want as it is not the same as the numeric value of that character. For that you need:

num.charAt(n) - '0'

These changes should allow you to correct your algorithm as it stands.

A more efficient approach would be to extract 5-digit sub-strings and convert them to integers. The first one would be:

Integer.parseInt(num.subString(0, 5));

You can iterate to get each one to find the greatest.

dave
  • 11,641
  • 5
  • 47
  • 65
  • 1
    It's `substring`, and it's `(0, n]`. The [Javadoc](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int-) says (in part) *The substring begins at the specified `beginIndex` and extends to the character at index `endIndex - 1`.* – Elliott Frisch Nov 19 '15 at 05:52
2

Although @ElliottFrisch and @dave provides more elegant answer, I tried to modify from your original version and here is my code (I have tested it):

public class ThousandDigits {
    public static void main(String[] args) {
        int greatest = 0;
        String num = ("73167176531330624919225119674426574742355349194934"
                + "96983520312774506326239578318016984801869478851843"
                + "85861560789112949495459501737958331952853208805511"
                + "12540698747158523863050715693290963295227443043557"
                + "66896648950445244523161731856403098711121722383113"
                + "62229893423380308135336276614282806444486645238749"
                + "30358907296290491560440772390713810515859307960866"
                + "70172427121883998797908792274921901699720888093776"
                + "65727333001053367881220235421809751254540594752243"
                + "52584907711670556013604839586446706324415722155397"
                + "53697817977846174064955149290862569321978468622482"
                + "83972241375657056057490261407972968652414535100474"
                + "82166370484403199890008895243450658541227588666881"
                + "16427171479924442928230863465674813919123162824586"
                + "17866458359124566529476545682848912883142607690042"
                + "24219022671055626321111109370544217506941658960408"
                + "07198403850962455444362981230987879927244284909188"
                + "84580156166097919133875499200524063689912560717606"
                + "05886116467109405077541002256983155200055935729725"
                + "71636269561882670428252483600823257530420752963450");

        int max = -1;
        for (int n = 0; n < num.length() - 4; n++) {
            greatest = ((num.charAt(n) - '0') * 10000 + (num.charAt(n + 1) - '0') * 1000
                    + (num.charAt(n + 2) - '0') * 100 + (num.charAt(n + 3) - '0') * 10 + (num.charAt(n + 4) - '0'));
            if (max < greatest) {
                max = greatest;
            }
        }
        System.out.print(max);
    }
}
Tsung-Ting Kuo
  • 1,171
  • 6
  • 16
  • 21
  • 1
    Why would you multiply the num.charAt by 1000 for the first 2 then 100 for the third one and then only 10 for the last one? – Frank Nov 19 '15 at 05:53
  • 1
    @Frank Because from your question I believe we are finding the largest 5 digits (e.g., 99890), not the sum of the 5 digits (e.g., 9 + 9 + 8 + 9 + 0 = 35). Therefore, we need to reconstruct the number by regarding the first 9 as 90000 (that's why multiply it by 10000), the second 9 as 9000, and so on. Does this make sense? – Tsung-Ting Kuo Nov 19 '15 at 05:55