Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String with 1000 digits, find the biggest 5 digits without an array

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
like image 779
Frank Avatar asked Dec 20 '25 16:12

Frank


2 Answers

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.

like image 129
Elliott Frisch Avatar answered Dec 22 '25 07:12

Elliott Frisch


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 +)));
like image 33
jbhardwaj Avatar answered Dec 22 '25 08:12

jbhardwaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!