-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyfuns.py
More file actions
87 lines (77 loc) · 1.72 KB
/
myfuns.py
File metadata and controls
87 lines (77 loc) · 1.72 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from itertools import count
from scipy.constants import golden
# Postpone Method - based on Will Ness
def genPrimes(mer=False):
yield 2
yield 3
yield 5
yield 7
D = {}
ps = genPrimes()
p = next(ps) and next(ps)
q = p ** 2
for c in count(9, 2):
s = D.pop(c, None)
if s:
pass
elif c < q:
yield c
continue
else:
s = count(q + p*2, p*2)
p = next(ps)
q = p ** 2
for m in s:
if m not in D:
break
D[m] = s
# Non-Postpone Method - based on David Eppstein
# def genPrimes():
# yield 2
# D = {}
# for q in count(3, 2):
# p = D.pop(q, None)
# if p:
# x = p + q
# while x in D:
# x += p
# D[x] = p
# else:
# D[q * q] = 2 * q
# yield q
def genTris():
for i in count(1):
yield i * (i + 1) // 2
def primeFactors(N):
factors = []
for p in genPrimes():
if p ** 2 > N:
factors.append((N, 1))
break
i = 0
while N % p == 0:
i += 1
N //= p
if i:
factors.append((p, i))
if N == 1: break
return factors
def divisors(n):
out = set()
step = 2 if n % 2 else 1
for i in range(1, int(n**.5) + 1, step):
if n % i == 0:
out.add(i)
out.add(n//i)
return out
def genFib(n = None):
if not n:
a, b = 1, 1
while True:
yield a
a, b = b, a + b
else:
try:
yield round(golden**n / 5**0.5)
except OverflowError:
yield 'Error: n too large'