File tree 2 files changed +38
-0
lines changed
project_euler/problem_095
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Problem 95
3
+ Url: https://projecteuler.net/problem=95
4
+ """
5
+ def find_smallest_member (n : int ) -> int :
6
+ """
7
+ Returns the smallest member of the longest amicable chain with no element exceeding one million
8
+ >> 14316
9
+ """
10
+
11
+ sum_of_div = [0 ] * (n + 1 )
12
+ for i in range (1 , n // 2 + 1 ):
13
+ for j in range (i * 2 , n + 1 , i ):
14
+ sum_of_div [j ] += i
15
+
16
+ checked = [False ] * (n + 1 )
17
+ max_len_of_chain = 0
18
+ result = 0
19
+ for i in range (2 , n + 1 ):
20
+ possible_chain = []
21
+ j = i
22
+ while not checked [j ]:
23
+ checked [j ] = True
24
+ possible_chain .append (j )
25
+ j = sum_of_div [j ]
26
+ if j > n :
27
+ break
28
+ if j in possible_chain :
29
+ len_of_chain = len (possible_chain ) - possible_chain .index (j )
30
+ if len_of_chain > max_len_of_chain :
31
+ max_len_of_chain = len_of_chain
32
+ result = min (possible_chain [- len_of_chain :])
33
+ break
34
+ return result
35
+
36
+
37
+ if __name__ == "__main__" :
38
+ print (f"Solution : { find_smallest_member (10 ** 6 )} " )
You can’t perform that action at this time.
0 commit comments