Summation of primes: Difference between revisions
m
→Python :: Functional: Fractionally faster series of primes
(→Python :: Functional: Added a more efficient variant (defined over a generator of primes)) |
m (→Python :: Functional: Fractionally faster series of primes) |
||
Line 414:
<lang python>'''Summatiom of primes'''
from itertools import count, takewhile
Line 433:
# ----------------------- GENERIC ------------------------
# enumFromThen :: Int -> Int -> [Int]
def enumFromThen(m):
'''A non-finite stream of integers
starting at m, and continuing
at the interval between m and n.
'''
return lambda n: count(m, n - m)
# primes :: [Int]
def primes():
'''An
for q in enumFromThen(3)(5):
while True:▼
seen[q ** 2] =
else:
lambda x: x + 2 * p,
q + 2 * p
)
] = p
# until :: (a -> Bool) -> (a -> a) -> a -> a
def until(p, f, v):
'''The result of repeatedly applying f until p holds.
The initial seed value is x.
'''
v = f(v)
return v
|