-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamicable_numbers.py
More file actions
64 lines (34 loc) · 1.56 KB
/
Copy pathamicable_numbers.py
File metadata and controls
64 lines (34 loc) · 1.56 KB
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
def sum_of_divisors(n):
# Calculates the sum of proper divisors for a given number 'n'
divisors_sum = 1
#Starting the factoring algorithm
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
divisors_sum += i
#A fall back mechnism to avoid prime numbers
if i != n // i:
divisors_sum += n // i
## Algorithm Ended
return divisors_sum
#Initializing the amicable numbers function
def find_amicable_numbers(limit):
amicable_numbers = []
for num in range(2, limit + 1):
sum_a = sum_of_divisors(num)
sum_b = sum_of_divisors(sum_a)
if num == sum_b and num != sum_a:
#to check for correlation and evade prime numbers
amicable_numbers.append((num, sum_a))
#print((num, sum_a))
## Uncoment the print statement above for verbosity
## i.e giving result in real time
return amicable_numbers
##===================================================
limit = 10000 # Set the upper limit for the range of numbers to check
## Feel free to modify the limit. - but dont set it too high as not to consume
## your CPU resources; 10000 is gentlemanly enough TONY.
amicable_nums = find_amicable_numbers(limit)
print("Amicable numbers:")
for index, pair in enumerate(amicable_nums):
print(index, '\t', pair)
# Set the upper limit for the range of numbers to check amicable_nums = find_amicable_numbers(limit) print("Amicable numbers:") for pair in amicable_nums: print(pair)