Improved generators and the coverage of the tests.

This commit is contained in:
Cédric Bonhomme 2017-05-05 09:46:12 +02:00
parent 385dc39753
commit 98682932b5
No known key found for this signature in database
GPG key ID: A1CB94DE57B7A70D
2 changed files with 57 additions and 27 deletions

View file

@ -56,13 +56,12 @@ def fermat() -> Iterator[int]:
y = pow(y-1,2)+1 y = pow(y-1,2)+1
def mersenne() -> Iterator[int]: def mersenne() -> Iterator[int]:
"""Generate 2^n - 1. """Generate 2^p - 1, where p is prime.
https://oeis.org/A001348 https://oeis.org/A001348
""" """
y = 3 prime_numbers = eratosthenes()
while True: while True:
yield y yield 2**next(prime_numbers) - 1
y = 2*y + 1
def eratosthenes() -> Iterator[int]: def eratosthenes() -> Iterator[int]:
"""Generate the prime numbers with the sieve of Eratosthenes. """Generate the prime numbers with the sieve of Eratosthenes.
@ -89,7 +88,8 @@ def composite() -> Iterator[int]:
p1 = p2 p1 = p2
def carmichael() -> Iterator[int]: def carmichael() -> Iterator[int]:
"""Composite numbers n such that a^(n-1) == 1 (mod n) for every a coprime to n. """Composite numbers n such that a^(n-1) == 1 (mod n) for every a coprime
to n.
https://oeis.org/A002997 https://oeis.org/A002997
""" """
for m in composite(): for m in composite():
@ -99,7 +99,7 @@ def carmichael() -> Iterator[int]:
else: else:
yield m yield m
def ackermann(m: int, n: int) -> int: def ackermann_naive(m: int, n: int) -> int:
"""Ackermann number. """Ackermann number.
""" """
if m == 0: if m == 0:
@ -109,6 +109,24 @@ def ackermann(m: int, n: int) -> int:
else: else:
return ackermann(m - 1, ackermann(m, n - 1)) return ackermann(m - 1, ackermann(m, n - 1))
def ackermann(m: int, n: int) -> int:
"""Ackermann number.
"""
while m >= 4:
if n == 0:
n = 1
else:
n = ackermann(m, n - 1)
m -= 1
if m == 3:
return (1 << n + 3) - 3
elif m == 2:
return (n << 1) + 3
elif m == 1:
return n + 2
else:
return n + 1
def fibonacci() -> Iterator[int]: def fibonacci() -> Iterator[int]:
"""Generate the sequence of Fibonacci. """Generate the sequence of Fibonacci.
https://oeis.org/A000045 https://oeis.org/A000045
@ -118,18 +136,6 @@ def fibonacci() -> Iterator[int]:
yield a yield a
a, b = b, a + b a, b = b, a + b
def syracuse(l: int = 15) -> Iterator[int]:
"""Generate the sequence of Syracuse.
"""
y = l
while True:
yield y
q,r = divmod(y,2)
if r == 0:
y = q
else:
y = 3*y + 1
def log_gen() -> Iterator[int]: def log_gen() -> Iterator[int]:
"""Logarithmic generator. """Logarithmic generator.
""" """

File diff suppressed because one or more lines are too long