Project Euler #95: Amicable chains

Question
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.

Answer : 14316

Hacker Rank Problem

Solution


 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.ArrayList;
import java.util.Scanner;

public class Solution {
    
    public static void main(String[] args) {
        try(Scanner sc = new Scanner(System.in)) {
            int N = sc.nextInt();
            ArrayList<Integer> primes = new ArrayList<>();
            primes.add(2);
            for(int i = 3; i <= N; i++) {
                boolean isPrime = true;
                for(int p : primes) {
                    if((p * p) > i) {
                        break;
                    }
                    if((i % p) == 0) {
                        isPrime = false;
                        break;
                    }
                }
                if(isPrime) {
                    primes.add(i);
                }
            }
            int[] divSum = new int[N + 1];
            for(int i = 2; i <= N; i++) {
                int sum = 1;
                int reduce = i;
                for(int p : primes) {
                    if((p * p) > reduce) {
                        break;
                    }
                    int factor = 1;
                    while((reduce % p) == 0) {
                        reduce /= p;
                        factor *= p;
                        factor++;
                    }
                    sum *= factor;
                }
                if(reduce > 1 && reduce < i) {
                    sum *= reduce + 1;
                }
                if(sum > 1) {
                    sum -= i;
                }
                divSum[i] = sum;
            }
            int longestChain = 0;
            int smallestMember = N;
            for(int i = 1; i <= N; i++) {
                ArrayList<Integer> chain = new ArrayList<>();
                chain.clear();
                chain.add(i);
                while(true) {
                    int add = divSum[chain.get(chain.size() - 1)];
                    chain.add(add);
                    if(add == i) {
                        break;
                    }
                    if(add < i) {
                        break;
                    }
                    if(add > N) {
                        break;
                    }
                    boolean isLoop = false;
                    for(int j = 1; j < chain.size() - 1; j++) {
                        if(add == chain.get(j)) {
                            isLoop = true;
                            break;
                        }
                    }
                    if(isLoop) {
                        break;
                    }
                }
                if(chain.get(chain.size() - 1) == i) {
                    if(chain.size() >= longestChain) {
                        if(longestChain < chain.size()) {
                            longestChain = chain.size();
                            smallestMember = chain.get(0);
                        }
                    }
                }
            }
            System.out.println(smallestMember);
        }
    }
}

Comments

Popular Posts