|
| 1 | +/** |
| 2 | + * Title: Largest product in a series |
| 3 | + * |
| 4 | + * The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. |
| 5 | + * 73167176531330624919225119674426574742355349194934 |
| 6 | + * 96983520312774506326239578318016984801869478851843 |
| 7 | + * 85861560789112949495459501737958331952853208805511 |
| 8 | + * 12540698747158523863050715693290963295227443043557 |
| 9 | + * 66896648950445244523161731856403098711121722383113 |
| 10 | + * 62229893423380308135336276614282806444486645238749 |
| 11 | + * 30358907296290491560440772390713810515859307960866 |
| 12 | + * 70172427121883998797908792274921901699720888093776 |
| 13 | + * 65727333001053367881220235421809751254540594752243 |
| 14 | + * 52584907711670556013604839586446706324415722155397 |
| 15 | + * 53697817977846174064955149290862569321978468622482 |
| 16 | + * 83972241375657056057490261407972968652414535100474 |
| 17 | + * 82166370484403199890008895243450658541227588666881 |
| 18 | + * 16427171479924442928230863465674813919123162824586 |
| 19 | + * 17866458359124566529476545682848912883142607690042 |
| 20 | + * 24219022671055626321111109370544217506941658960408 |
| 21 | + * 07198403850962455444362981230987879927244284909188 |
| 22 | + * 84580156166097919133875499200524063689912560717606 |
| 23 | + * 05886116467109405077541002256983155200055935729725 |
| 24 | + * 71636269561882670428252483600823257530420752963450 |
| 25 | + * Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? |
| 26 | + */ |
| 27 | +public class Problem8 { |
| 28 | + final String input = |
| 29 | + "73167176531330624919225119674426574742355349194934"+ |
| 30 | + "96983520312774506326239578318016984801869478851843"+ |
| 31 | + "85861560789112949495459501737958331952853208805511"+ |
| 32 | + "12540698747158523863050715693290963295227443043557"+ |
| 33 | + "66896648950445244523161731856403098711121722383113"+ |
| 34 | + "62229893423380308135336276614282806444486645238749"+ |
| 35 | + "30358907296290491560440772390713810515859307960866"+ |
| 36 | + "70172427121883998797908792274921901699720888093776"+ |
| 37 | + "65727333001053367881220235421809751254540594752243"+ |
| 38 | + "52584907711670556013604839586446706324415722155397"+ |
| 39 | + "53697817977846174064955149290862569321978468622482"+ |
| 40 | + "83972241375657056057490261407972968652414535100474"+ |
| 41 | + "82166370484403199890008895243450658541227588666881"+ |
| 42 | + "16427171479924442928230863465674813919123162824586"+ |
| 43 | + "17866458359124566529476545682848912883142607690042"+ |
| 44 | + "24219022671055626321111109370544217506941658960408"+ |
| 45 | + "07198403850962455444362981230987879927244284909188"+ |
| 46 | + "84580156166097919133875499200524063689912560717606"+ |
| 47 | + "05886116467109405077541002256983155200055935729725"+ |
| 48 | + "71636269561882670428252483600823257530420752963450"; |
| 49 | + |
| 50 | + public long solve() { |
| 51 | + long result = 0; |
| 52 | + char[] chars = input.toCharArray(); |
| 53 | + for (int i = 0; i < chars.length - 13; i++) { |
| 54 | + long product = calculate13Product(i, chars); |
| 55 | + if (product > result) { |
| 56 | + result = product; |
| 57 | + } |
| 58 | + } |
| 59 | + return result; |
| 60 | + } |
| 61 | + |
| 62 | + private long calculate13Product(int startPosition, char[] arr) { |
| 63 | + long result = 1; |
| 64 | + for (int i = startPosition; i < startPosition + 13; i++) { |
| 65 | + result *= arr[i] - '0'; // char to int |
| 66 | + } |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + public static void main(String[] args) { |
| 71 | + long result = new Problem8().solve(); |
| 72 | + System.out.println("Result: " + result); |
| 73 | + } |
| 74 | +} |
0 commit comments