Advertisement
Guest User

Untitled

a guest
Sep 30th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1.  
  2. public class Euler23 {
  3.  
  4. public static void main(String[] args) {
  5. Abundant tester = new Abundant(40);
  6.  
  7. tester.notAbundantSummableFiller();
  8. tester.abundantNumberChecker();
  9. tester.abundantNumberPrinter();
  10. // tester.nonAbundantSummablePrinter();
  11. System.out.println("total now: " + tester.getTotal());
  12.  
  13.  
  14. tester.abundantSummableCalculator();
  15. // tester.nonAbundantSummablePrinter();
  16.  
  17.  
  18. System.out.println("total: " + tester.notAbundantSummableSum());
  19.  
  20. }
  21.  
  22. }
  23.  
  24.  
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27.  
  28. public class Abundant {
  29.  
  30. private ArrayList<Integer> abundantNumbers;
  31. private Integer[] notAbundantSummable;
  32. private int originalSize;
  33. private int total;
  34.  
  35. public Abundant(int n) {
  36. this.abundantNumbers = new ArrayList();
  37. this.notAbundantSummable = new Integer[n];
  38. this.total = n * (n + 1) / 2;
  39. this.originalSize = n;
  40.  
  41. }
  42.  
  43. public void notAbundantSummableFiller() {
  44. for (int i = 1; i <= this.originalSize; i++) {
  45. this.notAbundantSummable[i - 1] = i;
  46. }
  47. }
  48.  
  49. public void nonAbundantSummablePrinter() {
  50. System.out.println(Arrays.toString(this.notAbundantSummable));
  51. }
  52.  
  53. public boolean isAbundantNumber(int n) {
  54. if (this.sumOfDivisors(n) > n) {
  55. return true;
  56. }
  57. return false;
  58. }
  59.  
  60. public void abundantNumberChecker() {
  61. for (int i = 1; i <= this.originalSize; i++) {
  62. if (this.isAbundantNumber(i)) {
  63. this.abundantNumbers.add(i);
  64. }
  65. }
  66. //System.out.println("size: " + this.abundantNumbers.size());
  67. }
  68.  
  69. public void abundantSummableCalculator() {
  70. int i = 1;
  71.  
  72. while (this.abundantNumbers.get(i - 1) <= this.originalSize / 2) {
  73.  
  74. for (int j = i; this.abundantNumbers.get(j - 1) <= (this.originalSize); j++) {
  75. int sum = this.abundantNumbers.get(j - 1) + this.abundantNumbers.get(i - 1);
  76. if (sum > this.originalSize || this.notAbundantSummable[Arrays.asList(notAbundantSummable).indexOf(sum)] == 0) {
  77. break;
  78. }
  79. else {
  80. this.nonAbundantSummablePrinter();
  81. System.out.println("sum: " + sum);
  82. System.out.println("index of sum: " + Arrays.asList(notAbundantSummable).indexOf(sum));
  83.  
  84.  
  85. this.notAbundantSummable[Arrays.asList(notAbundantSummable).indexOf(sum)] = 0;
  86.  
  87. //System.ou[Arrays.asList(notAbundantSummable).indexOf(sum)]t.println(this.total);
  88. }
  89. /*System.out.println("i: " + i + "; j: " + j);
  90. System.out.println("number to subtract: " + Integer.valueOf(this.abundantNumbers.get(i))
  91. + " " + Integer.valueOf(this.abundantNumbers.get(j)) + " " + this.);*/
  92. //this.notAbundantSummable.remove(Integer.valueOf(sum));
  93. //System.out.println("removed: " + (this.abundantNumbers.get(i) + this.abundantNumbers.get(j)));
  94. }
  95. i++;
  96. }
  97. }
  98.  
  99. public int getTotal() {
  100. return this.total;
  101. }
  102.  
  103. public int notAbundantSummableSum() {
  104. int sum = 0;
  105. //System.out.println("this: " + this.notAbundantSummable + " that: " + this.numberToSubtract);
  106. for (int i = 0; i < this.notAbundantSummable.length; i++) {
  107. sum = sum + this.notAbundantSummable[i];
  108. }
  109. return sum;
  110. }
  111.  
  112. public void abundantNumberPrinter() {
  113. for (int x : this.abundantNumbers) {
  114. System.out.println(x);
  115. }
  116. }
  117.  
  118. public int sumOfDivisors(int n) {
  119. int sum = 0;
  120.  
  121. if (n % 2 == 0) {
  122. for (int i = 2; i <= Math.sqrt(n); i++) {
  123. if (n % i == 0) {
  124. if (i == Math.sqrt(n)) {
  125. sum = sum + n / i;
  126. }
  127. else {
  128. sum = sum + n / i + i;
  129. }
  130. }
  131. }
  132. }
  133. else {
  134. for (int i = 3; i <= Math.sqrt(n); i = i + 2) {
  135. if (n % i == 0) {
  136. if (i == Math.sqrt(n)) {
  137. sum = sum + n / i;
  138. }
  139. else {
  140. sum = sum + n / i + i;
  141. }
  142. }
  143. }
  144. }
  145. return sum + 1;
  146. }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement