Skip to content

Commit 774e518

Browse files
authored
Merge pull request #268 from Arkadyuti30/master
README file for Project_Euler folder added
2 parents 4ead738 + 0fbd7fa commit 774e518

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-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.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Project Euler
2+
3+
----------------------------------------------------------------------------------------------
4+
5+
### What is Project Euler?
6+
7+
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.
8+
9+
The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.
10+
11+
12+
Click here to check out the problems: [Project Euler] (https://projecteuler.net/)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Euler
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+
/***
38+
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
39+
40+
2. The other number of the amicable pair is added when it appears in the loop later and checking is done for it ***/
41+
42+
if((n != x) && (n == y))
43+
sum_amicable += n;
44+
45+
} //End for
46+
47+
System.out.println("Sum of amicable numbers less than 10000 is:\t"+sum_amicable);
48+
} //End main()
49+
} //End class
1.14 KB
Binary file not shown.

0 commit comments

Comments
 (0)