Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package euler_21;
- /**
- *
- * @author stevengreen22
- */
- public class Euler_21 {
- /**
- * Let d(n) be defined as the sum of proper divisors of n (numbers less than
- * n which divide evenly into n).
- *
- * If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair
- * and each of a and b are called amicable numbers.
- *
- * For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22,
- * 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1,
- * 2, 4, 71 and 142; so d(284) = 220.
- *
- * Evaluate the sum of all the amicable numbers under 10000.
- *
- * /*
- * TODO code application logic here
- i want to loop from 1 to 10000 with this number being the param of the method.
- i then want it to return the sum of above number.
- then use then sum as the parameter.
- if the sum of 2nd param == first param then save to array
- if not, increment loop
- not efficient as will be checking same numbers twice.
- *
- *
- */
- public static void main(String[] args) {
- long start = System.currentTimeMillis();
- int inLoopSum = 0;
- for (int i = 1; i < 10000; i++) {
- //set a var to equal the sum of the proper divisors
- int tempSum = sumOfProperDivisors(whatAreProperDivisorsOf(i));
- //set a var to calc sum of proper divosor of prev number
- int divOfTempSum = sumOfProperDivisors(whatAreProperDivisorsOf(tempSum));
- //check if they are pairs
- if (areAmicablePair(i, tempSum, tempSum, divOfTempSum) == true) {
- int index = 0;
- //System.out.println("\ntest if true and output numbers");
- System.out.printf("num 1: %d num 2: %d sum1: %d sume2: %d", i, tempSum, tempSum, divOfTempSum);
- inLoopSum = inLoopSum + tempSum;
- System.out.println("\nsums : " + i);
- }
- }
- System.out.println("inLoopSum (Answer) ..." + inLoopSum +"\tTime: "+(System.currentTimeMillis()-start)+" ms");
- }
- /*
- * Method to take in an int and store all the proper divisors in an array
- * test purposes uses max int of 284
- */
- public static int[] whatAreProperDivisorsOf(int x) {
- //array to store the values
- int[] temp = new int[x / 2];
- int index = 0;
- for (int i = 1; i < x; i++) {
- if (x % i == 0) {
- temp[index] = i; //-1 as starting at one but index 0
- index++;
- }
- }
- return temp;
- }
- //simply add the divisors
- public static int sumOfProperDivisors(int[] a) {
- int sum = 0;
- for (int i : a) { //i gets each value of array
- sum += i;
- }
- return sum;
- }
- //compare the sums to see if amicable pair
- public static boolean areAmicablePair(int num1, int num2, int sum1, int sum2) {
- boolean test = false;
- //running total of all amicable pairs
- if (num1 != num2) {
- // int sum = 0;
- if ((num1 == sum2) && (num2 == sum1)) {
- // sum = sum + num1;
- test = true;
- }
- }
- // System.out.println("\nsum: "+sum);
- return test;
- }
- }
Add Comment
Please, Sign In to add comment