-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlucky_ntuples.py
66 lines (57 loc) · 1.55 KB
/
lucky_ntuples.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This will find the number of combinations of ntuples
# where each element divides the next. Only unique
# combinations are counted. Call answer( [yourlist], ntuple_len )
# to find how many ntuples there are. Useful for counting
# nprimes (primes with n divisors other than 1).
dic = {}
lucky = 0
def isqrt(n):
x = n
y = (x + 1) // 2
while y < x:
x = y
y = (x + n // x) // 2
return x
def answer(l, dmax):
global lucky
lucky = dmax
global dic
dic = {}
count = 0
for i in range(len(l)):
if str(l[i]) not in dic:
dic[str(l[i])] = 0
dic[str(l[i])] += 1
try:
if l[i] == l[i+1]:
continue
count += div(l[i], lucky)
except IndexError:
count += div(l[i], lucky)
return count
def div(n, d):
global dic
dic[str(n)] -= 1
if dic[str(n)] < 0:
dic[str(n)] += 1
return 0
elif d == 1:
dic[str(n)] += 1
return 1
elif str(n) + ',' + str(d) in dic:
if d == lucky:
dic[str(n) + ',' + str(d)] *= -1
else:
dic[str(n)] += 1
return dic[str(n) + ',' + str(d)]
else:
dic[str(n) + ',' + str(d)] = 0
sqrt = isqrt(n)
for i in range(1, sqrt+1):
if n%i == 0:
if str(i) in dic:
dic[str(n) + ',' + str(d)] += div(i, d-1)
if (i**2 != n) and (str(n//i) in dic):
dic[str(n) + ',' + str(d)] += div(n//i, d-1)
dic[str(n)] += 1
return dic[str(n) + ',' + str(d)]