Factorial primes: Difference between revisions

Content added Content deleted
m (Undo revision 338725 by Dr-neptune (talk) Erroneously overwrote Python)
Tag: Undo
m (Undo revision 338724 by Dr-neptune (talk) Erroneously overwrote Python)
Tag: Undo
Line 725: 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>
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|Racket}}==
=={{header|Python}}==
{{libheader|gmpy2}}
<syntaxhighlight lang="racket">
#lang racket
(require (only-in math/number-theory prime?))


This takes about 32 seconds to find the first 33 factorial primes on my machine (Ryzen 5 1500X).
(define (factorial-boundary-stream)
(define (factorial-stream-iter n curr-fact)
(stream-cons `(- ,n ,(sub1 curr-fact))
(stream-cons `(+ ,n ,(add1 curr-fact))
(factorial-stream-iter (add1 n) (* curr-fact (+ n 1))))))
(factorial-stream-iter 2 2))


<syntaxhighlight lang="python">
(define (format-large-number n)
from itertools import count
(let* ([num-chars (number->string n)]
from itertools import islice
[num-len (string-length num-chars)])
from typing import Iterable
(if (> num-len 40)
from typing import Tuple
(string-append
(substring num-chars 0 19)
"..."
(substring num-chars (- num-len 19) num-len)
(format " (total ~a digits)" num-len))
n)))


import gmpy2
(define (factorial-printer triple)
(let-values ([(op n fact) (apply values triple)])
(let ([fact (format-large-number fact)])
(displayln (format "~a! ~a 1 = ~a" n op fact)))))



(for ([i (in-stream
def factorials() -> Iterable[int]:
(stream-take
fact = 1
(stream-filter (λ (l) (prime? (third l))) (factorial-boundary-stream)) 14))]
[n (in-naturals 1)])
for i in count(1):
yield fact
(begin
fact *= i
(display (format "~a:\t" n))

(factorial-printer 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):
s = str(fact_prime)
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>
</syntaxhighlight>


{{out}}
<pre>
<pre>
First 33 factorial primes.
1: 2! + 1 = 3
2: 3! - 1 = 5
1! + 1 = 2
3: 3! + 1 = 7
2! + 1 = 3
4: 4! - 1 = 23
3! - 1 = 5
5: 6! - 1 = 719
3! + 1 = 7
6: 7! - 1 = 5039
4! - 1 = 23
7: 11! + 1 = 39916801
6! - 1 = 719
8: 12! - 1 = 479001599
7! - 1 = 5039
9: 14! - 1 = 87178291199
11! + 1 = 39916801
12! - 1 = 479001599
10: 27! + 1 = 10888869450418352160768000001
14! - 1 = 87178291199
11: 30! - 1 = 265252859812191058636308479999999
27! + 1 = 10888869450418352160768000001
12: 32! - 1 = 263130836933693530167218012159999999
30! - 1 = 265252859812191058636308479999999
13: 33! - 1 = 8683317618811886495518194401279999999
32! - 1 = 263130836933693530167218012159999999
14: 37! + 1 = 1376375309122634504...9581580902400000001 (total 44 digits)
33! - 1 = 8683317618811886495518194401279999999
15: 38! - 1 = 5230226174666011117...4100074291199999999 (total 45 digits)
16: 41! + 1 = 3345252661316380710...0751665152000000001 (total 50 digits)
37! + 1 = 13763753091226345046...79581580902400000001 (44 digits)
17: 73! + 1 = 4470115461512684340...3680000000000000001 (total 106 digits)
38! - 1 = 52302261746660111176...24100074291199999999 (45 digits)
18: 77! + 1 = 1451830920282858696...8000000000000000001 (total 114 digits)
41! + 1 = 33452526613163807108...40751665152000000001 (50 digits)
19: 94! - 1 = 1087366156656743080...9999999999999999999 (total 147 digits)
73! + 1 = 44701154615126843408...03680000000000000001 (106 digits)
20: 116! + 1 = 3393108684451898201...0000000000000000001 (total 191 digits)
77! + 1 = 14518309202828586963...48000000000000000001 (114 digits)
21: 154! + 1 = 3089769613847350887...0000000000000000001 (total 272 digits)
94! - 1 = 10873661566567430802...99999999999999999999 (147 digits)
22: 166! - 1 = 9003691705778437366...9999999999999999999 (total 298 digits)
116! + 1 = 33931086844518982011...00000000000000000001 (191 digits)
23: 320! + 1 = 2116103347219252482...0000000000000000001 (total 665 digits)
154! + 1 = 30897696138473508879...00000000000000000001 (272 digits)
24: 324! - 1 = 2288997460179102321...9999999999999999999 (total 675 digits)
166! - 1 = 90036917057784373664...99999999999999999999 (298 digits)
25: 340! + 1 = 5100864472103711080...0000000000000000001 (total 715 digits)
320! + 1 = 21161033472192524829...00000000000000000001 (665 digits)
26: 379! - 1 = 2484030746096470705...9999999999999999999 (total 815 digits)
324! - 1 = 22889974601791023211...99999999999999999999 (675 digits)
27: 399! + 1 = 1600863071165597381...0000000000000000001 (total 867 digits)
340! + 1 = 51008644721037110809...00000000000000000001 (715 digits)
28: 427! + 1 = 2906347176960734841...0000000000000000001 (total 940 digits)
379! - 1 = 24840307460964707050...99999999999999999999 (815 digits)
29: 469! - 1 = 6771809666814951090...9999999999999999999 (total 1051 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>
</pre>