Skip to content

Commit e3d92c5

Browse files
committed
Problem 21 (Sum of Amicable numbers under 10000) added to Project_Euler folder
1 parent 6068301 commit e3d92c5

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Binary file not shown.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//package Problem_21;
2+
class P21
3+
{ //Begin class
4+
5+
6+
/***** Function to calculate the sum of proper divisors or factors *****/
7+
public static int sumFactors(int num)
8+
{ //Begin sumFactors()
9+
int sum = 0;
10+
11+
for(int i = 1; i<num; i++)
12+
{ //Begin for
13+
if(num % i == 0)
14+
sum += i;
15+
} //End for
16+
17+
return sum;
18+
} //End sumFactors()
19+
20+
21+
22+
23+
public static void main(String args[])
24+
{ //Begin main()
25+
int sum_amicable = 0;
26+
int x = 0;
27+
int y = 0;
28+
29+
30+
for(int n = 1; n<=10000; n++)
31+
{ //Begin for
32+
33+
x = sumFactors(n); //Sum of factors or divisors of n
34+
y = sumFactors(x); //Sum of factors or divisors of x
35+
36+
/***
37+
1. Checking if the number is not equal to sum of its factors and if the number is equal to y(sum of factors of x) then the number is added to the sum of amicable numbers
38+
39+
2. The other number of the amicable pair is added when it appears in the loop later and checking is done for it ***/
40+
41+
if((n != x) && (n == y))
42+
sum_amicable += n;
43+
44+
} //End for
45+
46+
System.out.println("Sum of amicable numbers less than 10000 is:\t"+sum_amicable);
47+
} //End main()
48+
} //End class
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Amicable numbers
2+
3+
----------------------------------------------------------------------------------------------
4+
5+
### Problem 21
6+
7+
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
8+
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.
9+
10+
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.
11+
12+
Evaluate the sum of all the amicable numbers under 10000.

0 commit comments

Comments
 (0)