Factorial primes: Difference between revisions

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|PythonRacket}}==
<syntaxhighlight lang="pythonracket">
{{libheader|gmpy2}}
#lang racket
(require (only-in math/number-theory prime?))
 
(define (factorial-boundary-stream)
This takes about 32 seconds to find the first 33 factorial primes on my machine (Ryzen 5 1500X).
(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))
 
(define (format-large-number n)
<syntaxhighlight lang="python">
(let* ([num-chars (number->string n)]
from itertools import count
[num-len (string-length num-chars)])
from itertools import islice
(if (> num-len 40)
from typing import Iterable
(string-append
from typing import Tuple
(substring num-chars 0 19)
fact *= i"..."
(substring num-chars (- num-len 19) num-len)
(format " (total ~a digits)" num-len))
yield factn)))
 
(define (factorial-printer triple)
import gmpy2
(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
 
s = str(fact_prime)stream-take
def factorials() -> Iterable[int]:
(stream-filter (λ (l) (prime? (third l))) (factorial-boundary-stream)) 14))]
fact = 1
for i in[n count(in-naturals 1):])
(begin
yield fact
(display (format "~a:\t" n))
fact *= i
(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>
 
{{out}}
<pre>
1: 2! + 1 = 3
First 33 factorial primes.
12: 3! +- 1 = 25
23: 3! + 1 = 37
34: 4! - 1 = 523
35: 6! +- 1 = 7719
46: 7! - 1 = 235039
67: 11! -+ 1 = 71939916801
78: 12! - 1 = 5039479001599
119: 14! +- 1 = 3991680187178291199
10: 27! + 1 = 10888869450418352160768000001
12! - 1 = 479001599
11: 30! - 1 = 265252859812191058636308479999999
14! - 1 = 87178291199
12: 32! - 1 = 263130836933693530167218012159999999
27! + 1 = 10888869450418352160768000001
13: 33! - 1 = 8683317618811886495518194401279999999
30! - 1 = 265252859812191058636308479999999
14: 37! + 1 = 1376375309122634504...9581580902400000001 (total 44 digits)
32! - 1 = 263130836933693530167218012159999999
15: 38! - 1 = 5230226174666011117...4100074291199999999 (total 45 digits)
33! - 1 = 8683317618811886495518194401279999999
3716: 41! + 1 = 137637530912263450463345252661316380710...795815809024000000010751665152000000001 (44total 50 digits)
3817: 73! -+ 1 = 523022617466601111764470115461512684340...241000742911999999993680000000000000001 (45total 106 digits)
4118: 77! + 1 = 334525266131638071081451830920282858696...407516651520000000018000000000000000001 (50total 114 digits)
7319: 94! +- 1 = 447011546151268434081087366156656743080...036800000000000000019999999999999999999 (106total 147 digits)
7720: 116! + 1 = 145183092028285869633393108684451898201...480000000000000000010000000000000000001 (114total 191 digits)
9421: 154! -+ 1 = 108736615665674308023089769613847350887...999999999999999999990000000000000000001 (147total 272 digits)
11622: 166! +- 1 = 339310868445189820119003691705778437366...000000000000000000019999999999999999999 (191total 298 digits)
15423: 320! + 1 = 308976961384735088792116103347219252482...000000000000000000010000000000000000001 (272total 665 digits)
16624: 324! - 1 = 900369170577843736642288997460179102321...999999999999999999999999999999999999999 (298total 675 digits)
32025: 340! + 1 = 211610334721925248295100864472103711080...000000000000000000010000000000000000001 (665total 715 digits)
32426: 379! - 1 = 228899746017910232112484030746096470705...999999999999999999999999999999999999999 (675total 815 digits)
34027: 399! + 1 = 510086447210371108091600863071165597381...000000000000000000010000000000000000001 (715total 867 digits)
37928: 427! -+ 1 = 248403074609647070502906347176960734841...999999999999999999990000000000000000001 (815total 940 digits)
39929: 469! +- 1 = 160086307116559738156771809666814951090...000000000000000000019999999999999999999 (867total 1051 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>
 
8

edits