Guest User

Untitled

a guest
Jan 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package euler_21;
  6.  
  7. /**
  8.  *
  9.  * @author stevengreen22
  10.  */
  11. public class Euler_21 {
  12.  
  13.     /**
  14.      * Let d(n) be defined as the sum of proper divisors of n (numbers less than
  15.      * n which divide evenly into n).
  16.      *
  17.      * If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair
  18.      * and each of a and b are called amicable numbers.
  19.      *
  20.      * For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22,
  21.      * 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1,
  22.      * 2, 4, 71 and 142; so d(284) = 220.
  23.      *
  24.      * Evaluate the sum of all the amicable numbers under 10000.
  25.      *
  26.      *     /*
  27.       * TODO code application logic here
  28.         i want to loop from 1 to 10000 with this number being the param of the method.
  29.         i then want it to return the sum of above number.
  30.         then use then sum as the parameter.
  31.         if the sum of 2nd param == first param then save to array
  32.         if not, increment loop
  33.         not efficient as will be checking same numbers twice.
  34.  
  35.      *
  36.      *
  37.      */
  38.     public static void main(String[] args) {
  39.      
  40.        long start = System.currentTimeMillis();
  41.        
  42.         int inLoopSum = 0;
  43.         for (int i = 1; i < 10000; i++) {
  44.             //set a var to equal the sum of the proper divisors
  45.             int tempSum = sumOfProperDivisors(whatAreProperDivisorsOf(i));
  46.             //set a var to calc sum of proper divosor of prev number
  47.             int divOfTempSum = sumOfProperDivisors(whatAreProperDivisorsOf(tempSum));
  48.             //check if they are pairs
  49.             if (areAmicablePair(i, tempSum, tempSum, divOfTempSum) == true) {
  50.                 int index = 0;
  51.                 //System.out.println("\ntest if true and output numbers");
  52.                 System.out.printf("num 1: %d num 2: %d sum1: %d sume2: %d", i, tempSum, tempSum, divOfTempSum);
  53.                 inLoopSum = inLoopSum + tempSum;
  54.                 System.out.println("\nsums : " + i);
  55.             }
  56.         }
  57.  
  58.         System.out.println("inLoopSum (Answer) ..." + inLoopSum +"\tTime: "+(System.currentTimeMillis()-start)+" ms");
  59.     }
  60.     /*
  61.      * Method to take in an int and store all the proper divisors in an array
  62.      * test purposes uses max int of 284
  63.      */
  64.     public static int[] whatAreProperDivisorsOf(int x) {
  65.         //array to store the values
  66.         int[] temp = new int[x / 2];
  67.         int index = 0;
  68.  
  69.         for (int i = 1; i < x; i++) {
  70.             if (x % i == 0) {
  71.                 temp[index] = i;  //-1 as starting at one but index 0
  72.                 index++;
  73.             }
  74.         }
  75.         return temp;
  76.     }
  77.  
  78.     //simply add the divisors
  79.     public static int sumOfProperDivisors(int[] a) {
  80.         int sum = 0;
  81.         for (int i : a) {    //i gets each value of array
  82.             sum += i;
  83.         }
  84.         return sum;
  85.     }
  86.  
  87.     //compare the sums to see if amicable pair
  88.     public static boolean areAmicablePair(int num1, int num2, int sum1, int sum2) {
  89.  
  90.         boolean test = false;
  91.         //running total of all amicable pairs
  92.         if (num1 != num2) {
  93.             // int sum = 0;
  94.             if ((num1 == sum2) && (num2 == sum1)) {
  95.                 //  sum = sum + num1;
  96.                 test = true;
  97.             }
  98.         }
  99.         //   System.out.println("\nsum: "+sum);
  100.         return test;
  101.     }
  102. }
Add Comment
Please, Sign In to add comment