forked from Twiggecode/Integer-Sequences
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractical_numbers.py
40 lines (35 loc) · 888 Bytes
/
practical_numbers.py
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
import math
from itertools import combinations
def find_divisors(n):
divisors = []
i = 1
while i <= math.sqrt(n):
if n % i == 0:
if i == n/i:
divisors.append(i)
else:
divisors.extend([i, int(n/i)])
i += 1
divisors.sort()
divisors.pop()
return divisors
def check(num, arr):
combs = [elem for i in range(1, len(arr)+1) for elem in combinations(arr, i)]
return any([sum(c)==num for c in combs])
def is_practical(num):
divs = find_divisors(num)
for n in range(2, num):
if not check(n, divs):
return False
return True
def main():
n = int(input('Enter n: '))
count = -1
current = 0
while count < n:
current += 1
if is_practical(current):
count += 1
print(current)
if __name__ == '__main__':
main()