Factorial primes: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) (added Arturo) |
(Add Python) |
||
Line 724: | Line 724: | ||
<small>Items 15-17 are shown in full because that's still shorter than 20+length("...")+20+length(" (NN digits)").<br> |
<small>Items 15-17 are shown in full because that's still shorter than 20+length("...")+20+length(" (NN digits)").<br> |
||
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> |
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|Python}}== |
|||
{{libheader|gmpy2}} |
|||
Takes about 32 seconds to find the first 33 factorial primes on my machine (Ryzen 5 1500X). |
|||
<syntaxhighlight lang="python"> |
|||
from itertools import islice |
|||
from typing import Iterable |
|||
from typing import Tuple |
|||
import gmpy2 |
|||
def factorials() -> Iterable[int]: |
|||
i: int = 1 |
|||
n: int = 1 |
|||
while True: |
|||
yield n |
|||
n *= i |
|||
i += 1 |
|||
def factorial_primes() -> Tuple[int, int, str]: |
|||
for i, n in enumerate(factorials()): |
|||
if gmpy2.is_prime(n - 1): |
|||
yield (i, n - 1, "-") |
|||
if gmpy2.is_prime(n + 1): |
|||
yield (i, n + 1, "+") |
|||
def main(n=10) -> None: |
|||
print(f"First {n} factorial primes.") |
|||
for i, factorial_prime, op in islice(factorial_primes(), 1, n + 1): |
|||
s = str(factorial_prime) |
|||
if len(s) > 40: |
|||
s = f"{s[:20]}...{s[-20:]} ({len(s)} digits)" |
|||
print(f"{i}! {op} 1 = {s}") |
|||
if __name__ == "__main__": |
|||
import sys |
|||
main(int(sys.argv[1]) if len(sys.argv) > 1 else 10) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 33 factorial primes. |
|||
1! + 1 = 2 |
|||
2! + 1 = 3 |
|||
3! - 1 = 5 |
|||
3! + 1 = 7 |
|||
4! - 1 = 23 |
|||
6! - 1 = 719 |
|||
7! - 1 = 5039 |
|||
11! + 1 = 39916801 |
|||
12! - 1 = 479001599 |
|||
14! - 1 = 87178291199 |
|||
27! + 1 = 10888869450418352160768000001 |
|||
30! - 1 = 265252859812191058636308479999999 |
|||
32! - 1 = 263130836933693530167218012159999999 |
|||
33! - 1 = 8683317618811886495518194401279999999 |
|||
37! + 1 = 13763753091226345046...79581580902400000001 (44 digits) |
|||
38! - 1 = 52302261746660111176...24100074291199999999 (45 digits) |
|||
41! + 1 = 33452526613163807108...40751665152000000001 (50 digits) |
|||
73! + 1 = 44701154615126843408...03680000000000000001 (106 digits) |
|||
77! + 1 = 14518309202828586963...48000000000000000001 (114 digits) |
|||
94! - 1 = 10873661566567430802...99999999999999999999 (147 digits) |
|||
116! + 1 = 33931086844518982011...00000000000000000001 (191 digits) |
|||
154! + 1 = 30897696138473508879...00000000000000000001 (272 digits) |
|||
166! - 1 = 90036917057784373664...99999999999999999999 (298 digits) |
|||
320! + 1 = 21161033472192524829...00000000000000000001 (665 digits) |
|||
324! - 1 = 22889974601791023211...99999999999999999999 (675 digits) |
|||
340! + 1 = 51008644721037110809...00000000000000000001 (715 digits) |
|||
379! - 1 = 24840307460964707050...99999999999999999999 (815 digits) |
|||
399! + 1 = 16008630711655973815...00000000000000000001 (867 digits) |
|||
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> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |