**Problem:**

The proper divisors of a number are all the divisors excluding the number itself. For example, the proper divisors of 28 are 1, 2, 4, 7, and 14. As the sum of these divisors is equal to 28, we call it a perfect number.

Interestingly the sum of the proper divisors of 220 is 284 and the sum of the proper divisors of 284 is 220, forming a chain of two numbers. For this reason, 220 and 284 are called an amicable pair.

Perhaps less well known are longer chains. For example, starting with 12496, we form a chain of five numbers:

12496 [→] 14288 [→] 15472 [→] 14536 [→] 14264 ([→] 12496 [→] ...)

Since this chain returns to its starting point, it is called an amicable chain.

Find the smallest member of the longest amicable chain with no element exceeding one million.

Interestingly the sum of the proper divisors of 220 is 284 and the sum of the proper divisors of 284 is 220, forming a chain of two numbers. For this reason, 220 and 284 are called an amicable pair.

Perhaps less well known are longer chains. For example, starting with 12496, we form a chain of five numbers:

12496 [→] 14288 [→] 15472 [→] 14536 [→] 14264 ([→] 12496 [→] ...)

Since this chain returns to its starting point, it is called an amicable chain.

Find the smallest member of the longest amicable chain with no element exceeding one million.

**Solution:**

1533776805

**Code:**

The solution may include methods that will be found here: Library.java .

The solution may include methods that will be found here: Library.java .

public interface EulerSolution{

public String run();

}

/*

* Solution to Project Euler problem 45

* By Nayuki Minase

*

* http://nayuki.eigenstate.org/page/project-euler-solutions

* https://github.com/nayuki/Project-Euler-solutions

*/

public final class p045 implements EulerSolution {

public static void main(String[] args) {

System.out.println(new p045().run());

}

public String run() {

int i = 286;

int j = 166;

int k = 144;

while (true) {

long triangle = (long)i * (i + 1) / 2;

long pentagon = (long)j * (j * 3 - 1) / 2;

long hexagon = (long)k * (k * 2 - 1);

long min = Math.min(Math.min(triangle, pentagon), hexagon);

if (min == triangle && min == pentagon && min == hexagon)

return Long.toString(min);

if (min == triangle) i++;

if (min == pentagon) j++;

if (min == hexagon ) k++;

}

}

}

## No comments :

## Post a Comment