-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem21.java
58 lines (43 loc) · 1.07 KB
/
Problem21.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
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.
*/
package com.prog.ProjectEulerPrograms;
public class Problem21 {
public static void main(String[] args) {
int num = 1, sum = 0;
while(num != 10000) {
int a = findSum(num);
int b = getDivisors(a);
boolean result = checkForAmicablePair(a, b, num);
if(result)
sum += num;
num++;
}
System.out.println("Answer is: "+sum);
}
private static int getDivisors(int a) {
int sum = 0;
for(int i = 1; i<a; i++)
{
if(a % i == 0)
sum += i;
}
return sum;
}
private static int findSum(int num) {
int sum = 0;
for(int i = 1; i<num; i++)
{
if(num % i == 0)
sum += i;
}
return sum;
}
private static boolean checkForAmicablePair(int a, int b, int num) {
if(num == b && a != num)
return true;
return false;
}
}