Advertisement
Guest User

Untitled

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