Factorial primes: Difference between revisions
m
Undo revision 338724 by Dr-neptune (talk) Erroneously overwrote Python
Thundergnat (talk | contribs) m (Undo revision 338725 by Dr-neptune (talk) Erroneously overwrote Python) Tag: Undo |
Thundergnat (talk | contribs) m (Undo revision 338724 by Dr-neptune (talk) Erroneously overwrote Python) Tag: Undo |
||
Line 725:
Aside: Unfortunately the relative performance falls off a cliff under pwa/p2js by the 320! mark, and it'd probably need a few minutes to get to the 30th.</small>
=={{header|
{{libheader|gmpy2}}
<syntaxhighlight lang="racket">▼
This takes about 32 seconds to find the first 33 factorial primes on my machine (Ryzen 5 1500X).
from itertools import count
from itertools import islice
from typing import Iterable
from typing import Tuple
"..."▼
n)))▼
import gmpy2
def factorials() -> Iterable[int]:
(stream-take▼
fact = 1
for i
def factorial_primes() -> Iterable[Tuple[int, int, str]]:
for n, fact in enumerate(factorials()):
if gmpy2.is_prime(fact - 1):
yield (n, fact - 1, "-")
if gmpy2.is_prime(fact + 1):
yield (n, fact + 1, "+")
def print_factorial_primes(limit=10) -> None:
print(f"First {limit} factorial primes.")
for n, fact_prime, op in islice(factorial_primes(), 1, limit + 1):
if len(s) > 40:
s = f"{s[:20]}...{s[-20:]} ({len(s)} digits)"
print(f"{n}! {op} 1 = {s}")
if __name__ == "__main__":
import sys
print_factorial_primes(int(sys.argv[1]) if len(sys.argv) > 1 else 10)
</syntaxhighlight>
{{out}}
<pre>
First 33 factorial primes.
12! - 1 = 479001599
10: 27! + 1 = 10888869450418352160768000001▼
14! - 1 = 87178291199
11: 30! - 1 = 265252859812191058636308479999999▼
12: 32! - 1 = 263130836933693530167218012159999999▼
13: 33! - 1 = 8683317618811886495518194401279999999▼
427! + 1 = 29063471769607348411...00000000000000000001 (940 digits)
469! - 1 = 67718096668149510900...99999999999999999999 (1051 digits)
546! - 1 = 14130200926141832545...99999999999999999999 (1260 digits)
872! + 1 = 19723152008295244962...00000000000000000001 (2188 digits)
974! - 1 = 55847687633820181096...99999999999999999999 (2490 digits)
</pre>
|