Project Euler 95 - Amicable Chains

Official link: https://projecteuler.net/problem=95

Thought Process

First, generate an array (called values) that is 1 + 10^6 long which looks like the following: values = [0,1,1,1,1, .... ,1,1,1]

We will be building up the divisors of each number, n, where values[n] = sum of divisors n

Now create 2 nested loops, for variables x and y.

  1. x go from 2 to 10^6 / 2

  2. y go from 1 to 10^6 / x

loop through x then y, then values[x*y] += x, add a condiditon such that if x*y = x don't add it


This is because the index of values[x*y] is the number x*y, so clearly x is a divisor of x*y

For example lets calculate the sum of Divisors of 10:

We go through the loop and reach the following conditions

  1. x = 2, y = 5, x*y = 10, so the number 10 has the divisor 2, and values[10] = 1, so after addition values[10] = 3

  2. x = 5, y = 2, x*y = 10, so the number 10 has the divisor 5, and values[10] = 3, so after addition values[10] = 8

So now we see that for n=10, values[10] = 1 + 2 + 5 = 8 which are the divisors of 10!

From there we can create the chains easily.

I will include a Divisors Function in my Essential Functions page

Interactive Code

Enter a number (yourinput)

Code will output the minimum element of the longest chain with no element larger than yourinput