Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. /*
  2. * Tyler Young
  3. * Independent Project 1: Project Euler Q8
  4. * ICS4U1-2
  5. * October 19, 2014
  6. */
  7.  
  8. package eulerProblems;
  9.  
  10. import java.util.ArrayList;
  11.  
  12. public class Young_Tyler_Project1_Q8 {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. // Creates an array list called myArray
  17. ArrayList<Integer> myArray = new ArrayList<>();
  18.  
  19. // Sets Variables
  20. int counter = 0;
  21. int currentDig = 0;
  22. long product = 1;
  23. long max = 0;
  24.  
  25. // 1000 digit number stored as a string
  26. String num = "73167176531330624919225119674426574742355349194934"
  27. + "96983520312774506326239578318016984801869478851843"
  28. + "85861560789112949495459501737958331952853208805511"
  29. + "12540698747158523863050715693290963295227443043557"
  30. + "66896648950445244523161731856403098711121722383113"
  31. + "62229893423380308135336276614282806444486645238749"
  32. + "30358907296290491560440772390713810515859307960866"
  33. + "70172427121883998797908792274921901699720888093776"
  34. + "65727333001053367881220235421809751254540594752243"
  35. + "52584907711670556013604839586446706324415722155397"
  36. + "53697817977846174064955149290862569321978468622482"
  37. + "83972241375657056057490261407972968652414535100474"
  38. + "82166370484403199890008895243450658541227588666881"
  39. + "16427171479924442928230863465674813919123162824586"
  40. + "17866458359124566529476545682848912883142607690042"
  41. + "24219022671055626321111109370544217506941658960408"
  42. + "07198403850962455444362981230987879927244284909188"
  43. + "84580156166097919133875499200524063689912560717606"
  44. + "05886116467109405077541002256983155200055935729725"
  45. + "71636269561882670428252483600823257530420752963450";
  46.  
  47. // Loops through the 1000 digit number
  48. for (int i = 0; i < num.length(); i++) {
  49.  
  50. // Gets the current digit through converting it into an integer
  51. currentDig = Integer.parseInt("" + num.charAt(i));
  52.  
  53. // Adds the current digit to the array list
  54. myArray.add(currentDig);
  55.  
  56. // increments the counter by one
  57. counter++;
  58.  
  59. // Once the counter reaches 13, it will multiply and set the largest
  60. // product to the variable max
  61. if (counter == 13) {
  62.  
  63. // For each loop for my array list will get the product of the 13 digits
  64. // for everything in the set, going through each term
  65. for (int j : myArray) {
  66.  
  67. product = j * product;
  68. }
  69.  
  70. // Compares the current product to the max
  71. // will set to the max if the current product is larger
  72. if (product > max) {
  73.  
  74. max = product;
  75.  
  76. }
  77.  
  78. // Removes the first digit in the array list
  79. myArray.remove(0);
  80.  
  81. // Resets the product
  82. product = 1;
  83. }
  84.  
  85. // Sets the counter to max out at 13 digits (Arrays start at 0)
  86. counter = myArray.size();
  87.  
  88.  
  89. }
  90.  
  91. // Prints out the maximum product
  92. System.out.println(max);
  93.  
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement