Repository navigation
Expand file tree
/
Copy pathch02_ex1.py
More file actions
173 lines (149 loc) · 4.17 KB
/
Copy pathch02_ex1.py
File metadata and controls
173 lines (149 loc) · 4.17 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
"""Functional Python Programming
Chapter 2, Example Set 1
"""
def higher_order():
"""Higher Order Functions."""
year_cheese = [(2000, 29.87), (2001, 30.12), (2002, 30.6), (2003, 30.66),
(2004, 31.33), (2005, 32.62), (2006, 32.73), (2007, 33.5),
(2008, 32.84), (2009, 33.02), (2010, 32.92)
]
max( year_cheese )
max( year_cheese, key= lambda yc: yc[1] )
max(map(lambda yc: (yc[1],yc), year_cheese))
def namedtuples():
"""nametuple vs. class performance"""
import timeit
print( "class", timeit.timeit("""x= X(1,2,3)""",
"""
class X:
def __init__( self, a, b, c ):
self.a= a
self.b= b
self.c= c
""") )
print( "tuple", timeit.timeit("""x= (1,2,3)""") )
print( "namedtuple", timeit.timeit("""x= X(1,2,3)""",
"""
from collections import namedtuple
X= namedtuple( "X", ("a", "b", "c") )
""") )
def sum_non_strict():
"""Non-strict generators."""
def numbers():
for i in range(1024):
print( "=", i )
yield i
def sum_to(n):
sum= 0
for i in numbers():
if i == n: break
sum += i
return sum
import math
def isprimei( n ):
"""Is n prime?
>>> isprimei(2)
True
>>> tuple( isprimei(x) for x in range(3,11) )
(True, False, True, False, True, False, False, False)
"""
if n < 2: return False
if n == 2: return True
if n % 2 == 0: return False
for i in range(3,1+int(math.sqrt(n)),2):
if n % i == 0:
return False
return True
def isprimer(n):
"""Is n prime?
>>> isprimer(2)
True
>>> tuple( isprimer(x) for x in range(3,11) )
(True, False, True, False, True, False, False, False)
"""
def isprime(k, coprime):
"""Is k relatively prime to the value coprime?"""
if k < coprime*coprime: return True
if k % coprime == 0: return False
return isprime(k, coprime+2)
if n < 2: return False
if n == 2: return True
if n % 2 == 0: return False
return isprime(n, 3)
def isprimeg( n ):
"""Is n prime?
>>> isprimeg(2)
True
>>> tuple( isprimeg(x) for x in range(3,11) )
(True, False, True, False, True, False, False, False)
Remarkably slow for large primes, for example, M_61=2**61-1.
"""
if n < 2: return False
if n == 2: return True
if n % 2 == 0: return False
return not any(n%p==0 for p in range(3,int(math.sqrt(n))+1,2))
def recursion():
"""Recursion Performance Comparison.
"""
import timeit
print( "isprimei", timeit.timeit( "isprimei(131071)", """
import math
def isprimei( n ):
if n < 2: return False
if n == 2: return True
if n % 2 == 0: return False
for i in range(3,1+int(math.sqrt(n)),2):
if n % i == 0:
return False
return True
""", number=100000 ) )
print( "isprimer", timeit.timeit( """isprimer(131071)""", """
def isprimer( n ):
def isprime( n, coprime ):
if n < coprime*coprime: return True
if n % coprime == 0: return False
return isprime( n, coprime+2 )
if n < 2: return False
if n == 2: return True
if n % 2 == 0: return False
return isprime( n, 3 )
""", number=100000 ) )
print( "isprimeg", timeit.timeit( """isprimeg(131071)""", """
import math
def isprimeg( n ):
if n < 2: return False
if n == 2: return True
if n % 2 == 0: return False
return not any(n%p==0 for p in range(3,int(math.sqrt(n))+2))
""", number=100000 ) )
def limit_of_performance():
"""We can see that testing a large prime is
quite slow. Testing large non-primes is quite fast.
"""
import time
t= time.perf_counter()
for i in range(30,89):
m= 2**i-1
print( i, m, end="" )
if isprimeg( m ):
print( "prime", end="" )
else:
print( "composite", end="" )
print( time.perf_counter() - t )
def test():
"""
>>> isprimei(131071)
True
>>> isprimer(131071)
True
>>> isprimeg(131071)
True
"""
import doctest
doctest.testmod(verbose=2)
if __name__ == "__main__":
namedtuples()
#recursion()
#limit_of_performance()
test()