Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Euler23 {
- public static void main(String[] args) {
- Abundant tester = new Abundant(40);
- tester.notAbundantSummableFiller();
- tester.abundantNumberChecker();
- tester.abundantNumberPrinter();
- // tester.nonAbundantSummablePrinter();
- System.out.println("total now: " + tester.getTotal());
- tester.abundantSummableCalculator();
- // tester.nonAbundantSummablePrinter();
- System.out.println("total: " + tester.notAbundantSummableSum());
- }
- }
- import java.util.ArrayList;
- import java.util.Arrays;
- public class Abundant {
- private ArrayList<Integer> abundantNumbers;
- private Integer[] notAbundantSummable;
- private int originalSize;
- private int total;
- public Abundant(int n) {
- this.abundantNumbers = new ArrayList();
- this.notAbundantSummable = new Integer[n];
- this.total = n * (n + 1) / 2;
- this.originalSize = n;
- }
- public void notAbundantSummableFiller() {
- for (int i = 1; i <= this.originalSize; i++) {
- this.notAbundantSummable[i - 1] = i;
- }
- }
- public void nonAbundantSummablePrinter() {
- System.out.println(Arrays.toString(this.notAbundantSummable));
- }
- public boolean isAbundantNumber(int n) {
- if (this.sumOfDivisors(n) > n) {
- return true;
- }
- return false;
- }
- public void abundantNumberChecker() {
- for (int i = 1; i <= this.originalSize; i++) {
- if (this.isAbundantNumber(i)) {
- this.abundantNumbers.add(i);
- }
- }
- //System.out.println("size: " + this.abundantNumbers.size());
- }
- public void abundantSummableCalculator() {
- int i = 1;
- while (this.abundantNumbers.get(i - 1) <= this.originalSize / 2) {
- for (int j = i; this.abundantNumbers.get(j - 1) <= (this.originalSize); j++) {
- int sum = this.abundantNumbers.get(j - 1) + this.abundantNumbers.get(i - 1);
- if (sum > this.originalSize || this.notAbundantSummable[Arrays.asList(notAbundantSummable).indexOf(sum)] == 0) {
- break;
- }
- else {
- this.nonAbundantSummablePrinter();
- System.out.println("sum: " + sum);
- System.out.println("index of sum: " + Arrays.asList(notAbundantSummable).indexOf(sum));
- this.notAbundantSummable[Arrays.asList(notAbundantSummable).indexOf(sum)] = 0;
- //System.ou[Arrays.asList(notAbundantSummable).indexOf(sum)]t.println(this.total);
- }
- /*System.out.println("i: " + i + "; j: " + j);
- System.out.println("number to subtract: " + Integer.valueOf(this.abundantNumbers.get(i))
- + " " + Integer.valueOf(this.abundantNumbers.get(j)) + " " + this.);*/
- //this.notAbundantSummable.remove(Integer.valueOf(sum));
- //System.out.println("removed: " + (this.abundantNumbers.get(i) + this.abundantNumbers.get(j)));
- }
- i++;
- }
- }
- public int getTotal() {
- return this.total;
- }
- public int notAbundantSummableSum() {
- int sum = 0;
- //System.out.println("this: " + this.notAbundantSummable + " that: " + this.numberToSubtract);
- for (int i = 0; i < this.notAbundantSummable.length; i++) {
- sum = sum + this.notAbundantSummable[i];
- }
- return sum;
- }
- public void abundantNumberPrinter() {
- for (int x : this.abundantNumbers) {
- System.out.println(x);
- }
- }
- public int sumOfDivisors(int n) {
- int sum = 0;
- if (n % 2 == 0) {
- for (int i = 2; i <= Math.sqrt(n); i++) {
- if (n % i == 0) {
- if (i == Math.sqrt(n)) {
- sum = sum + n / i;
- }
- else {
- sum = sum + n / i + i;
- }
- }
- }
- }
- else {
- for (int i = 3; i <= Math.sqrt(n); i = i + 2) {
- if (n % i == 0) {
- if (i == Math.sqrt(n)) {
- sum = sum + n / i;
- }
- else {
- sum = sum + n / i + i;
- }
- }
- }
- }
- return sum + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement