All pastes #1213968 Raw Edit

Abundant numbers

public java v1 · immutable
#1213968 ·published 2008-09-29 19:59 UTC
rendered paste body
import java.util.*;public class Problem23 {   // Compute sum of proper divisors   public static int sumDivisors(int num) {      int sum = 1; //1 is a proper divisor      for(int i=2;i*i<num;i++) {         if(num % i == 0) {            sum += i;            sum += num / i;         }      }      return sum;   }   public static int[] getAbundant(int limit) {      int[] result = new int[50];      int index = 0;      for(int i=12;i<limit;i++) {         if(sumDivisors(i) > i) {            if(index == result.length) {               int[] tmp = new int[index*2];               for(int j=0;j<index;j++) {                  tmp[j] = result[j];               }               result = tmp;               result[index] = i;               index++;            } else {               result[index] = i;               index++;            }         }      }      int[] tmp = new int[index];      for(int i=0;i<index;i++) {         tmp[i] = result[i];      }      return tmp;   }   public static void main(String[] args) {      int sum = 0;      int[] abundant = getAbundant(28124); //Get all abundant numbers      for(int i=0;i<28124;i++) {         boolean done = false;         for(int j=0;j<abundant.length && abundant[j] < i && !done;j++) { //First part of pair            int p = abundant[j];            for(int k=j;k<abundant.length && abundant[k] < i && !done;k++) { //Second part of pair               if(p + abundant[k] == i) {                  System.out.println(i + " is the sum of "+p+" and "+abundant[k]);                  done = true;               }            }         }         if(!done) { //If no pair was found            System.out.println(i + "cannot be written as a sum of abundant numbers");            sum += i;         }      }      System.out.println(sum);   }}