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(28123);
- tester.notAbundantSummableFiller();
- tester.abundantNumberChecker();
- // tester.abundantNumberPrinter();
- tester.abundantSummableCalculator();
- System.out.println("total: " + tester.notAbundantSummableSum());
- }
- }
- import java.util.ArrayList;
- public class Abundant {
- private ArrayList<Integer> abundantNumbers;
- private ArrayList<Integer> notAbundantSummable;
- private int originalSize;
- public Abundant(int n) {
- this.abundantNumbers = new ArrayList();
- this.notAbundantSummable = new ArrayList();
- this.originalSize = n;
- }
- public void notAbundantSummableFiller() {
- for (int i = 1; i <= this.originalSize; i++) {
- this.notAbundantSummable.add(i);
- }
- }
- public boolean isAbundantNumber(int n) {
- if (this.sumOfDivisors(n) > n) {
- return true;
- }
- return false;
- }
- public void abundantNumberChecker() {
- for (int i = 1; i <= this.notAbundantSummable.size(); i++) {
- if (this.isAbundantNumber(i)) {
- this.abundantNumbers.add(i);
- }
- }
- }
- public void abundantSummableCalculator() {
- int i = 0;
- while (this.abundantNumbers.get(i) <= this.originalSize / 2) {
- for (int j = i; this.abundantNumbers.get(j) + this.abundantNumbers.get(i) <= (this.originalSize); j++) {
- /*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(this.abundantNumbers.get(i) + this.abundantNumbers.get(j)));
- //System.out.println("removed: " + (this.abundantNumbers.get(i) + this.abundantNumbers.get(j)));
- }
- i++;
- }
- }
- public int notAbundantSummableSum() {
- int sum = 0;
- //System.out.println("this: " + this.notAbundantSummable + " that: " + this.numberToSubtract);
- for(int x: this.notAbundantSummable){
- sum = sum + x;
- }
- 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