-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathseries.py
100 lines (69 loc) · 1.69 KB
/
series.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
"""
Solution to Project Euler Problems
http://projecteuler.net/
by Apalala <apalala@gmail.com>
(cc) Attribution-ShareAlike
http://creativecommons.org/licenses/by-sa/3.0/
Mthematical series.
"""
import math
from math import sqrt
from itertools import count
from memoization import memoize
@memoize
def factorial(n):
return math.factorial(n)
def all_fibs(limit=None):
a = b = 1
while limit is None or a < limit:
yield a
a, b = b, a + b
def fibs(n):
for i, f in enumerate(all_fibs()):
if i >= n:
break
yield f
def sum_of_squares(n):
return (n * (n + 1) * (2 * n + 1)) // 6
def square_of_sum(n):
return (n ** 2 * (n + 1) ** 2) // 4
def is_triangle(n):
assert n > 0
c = (-1 + sqrt(1 + 8 * n)) / 2
return c == int(c)
def triangles():
for n in count(1):
yield n * (n + 1) // 2
def is_pentagonal(n):
assert n > 0
c = (1 + sqrt(1 + 3 * 8 * n)) / 6
return c == int(c)
def pentagonal(n):
return n * (3 * n - 1) // 2
def pentagonals(upto=None):
for n in count(1):
p = pentagonal(n)
if upto is not None and p > upto:
break
yield p
def is_exagonal(n):
assert n > 0
c = (1 + sqrt(1 + 8 * n)) / 4
return c == int(c)
def exagonals():
for n in count(1):
yield n * (2 * n - 1)
def test():
assert is_triangle(6)
assert is_triangle(10)
assert not is_triangle(5)
assert is_pentagonal(5)
assert is_pentagonal(12)
assert is_exagonal(15)
assert not is_exagonal(10)
if __name__ == "__main__":
test()
print(list(pentagonals(1)))
print(list(pentagonals(5)))
print(list(pentagonals(100)))