forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp021.py
More file actions
30 lines (25 loc) · 927 Bytes
/
p021.py
File metadata and controls
30 lines (25 loc) · 927 Bytes
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
#
# Solution to Project Euler problem 21
# Copyright (c) Project Nayuki. All rights reserved.
#
# https://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
# We first compute a table of sum-of-proper-divisors, then we use it to test which numbers are amicable.
# This approach differs from the Java implementation because trying to directly compute
# the proper-divisor-sum of each number by brute force is unacceptably slow in Python.
def compute():
# Compute sum of proper divisors for each number
divisorsum = [0] * 10000
for i in range(1, len(divisorsum)):
for j in range(i * 2, len(divisorsum), i):
divisorsum[j] += i
# Find all amicable pairs within range
ans = 0
for i in range(1, len(divisorsum)):
j = divisorsum[i]
if j != i and j < len(divisorsum) and divisorsum[j] == i:
ans += i
return str(ans)
if __name__ == "__main__":
print(compute())