You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: project_euler/problem_095/sol1.py
+15-5
Original file line number
Diff line number
Diff line change
@@ -8,27 +8,31 @@
8
8
In this implementation, we aim to identify all amicable chains and find the one with the maximum length, while also returning the smallest member of that chain.
9
9
"""
10
10
11
+
11
12
defsum_of_proper_divisors(n):
12
13
"""Calculate the sum of proper divisors of n."""
13
14
ifn<2:
14
15
return0# Proper divisors of 0 and 1 are none.
15
16
total=1# Start with 1, since it is a proper divisor of any n > 1
16
17
sqrt_n=int(n**0.5) # Calculate the integer square root of n.
17
-
18
+
18
19
# Loop through possible divisors from 2 to the square root of n
19
20
foriinrange(2, sqrt_n+1):
20
21
ifn%i==0: # Check if i is a divisor of n
21
22
total+=i# Add the divisor
22
23
ifi!=n//i: # Avoid adding the square root twice
23
24
total+=n//i# Add the corresponding divisor (n/i)
24
-
25
+
25
26
returntotal
26
27
28
+
27
29
deffind_longest_amicable_chain(limit):
28
30
"""Find the smallest member of the longest amicable chain under a given limit."""
29
31
sum_divisors= {} # Dictionary to store the sum of proper divisors for each number
30
32
foriinrange(1, limit+1):
31
-
sum_divisors[i] =sum_of_proper_divisors(i) # Calculate and store sum of proper divisors
33
+
sum_divisors[i] =sum_of_proper_divisors(
34
+
i
35
+
) # Calculate and store sum of proper divisors
32
36
33
37
longest_chain= [] # To store the longest amicable chain found
34
38
seen= {} # Dictionary to track numbers already processed
0 commit comments