Factorial primes: Difference between revisions

m
Undo revision 338724 by Dr-neptune (talk) Erroneously overwrote Python
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:
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|RacketPython}}==
{{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="racketpython">
(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))]
for i [nin count(in-naturals 1)]):
n)))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(stream-takefact_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>
First 33 factorial primes.
1: 2! + 1 = 3
2: 31! -+ 1 = 52
3: 32! + 1 = 73
4: 43! - 1 = 235
5: 63! -+ 1 = 7197
6: 74! - 1 = 503923
7: 116! +- 1 = 39916801719
8: 127! - 1 = 4790015995039
9: 1411! -+ 1 = 8717829119939916801
12! - 1 = 479001599
10: 27! + 1 = 10888869450418352160768000001
14! - 1 = 87178291199
11: 30! - 1 = 265252859812191058636308479999999
10: 27! + 1 = 10888869450418352160768000001
12: 32! - 1 = 263130836933693530167218012159999999
11: 30! - 1 = 265252859812191058636308479999999
13: 33! - 1 = 8683317618811886495518194401279999999
12: 32! - 1 = 263130836933693530167218012159999999
14: 37! + 1 = 1376375309122634504...9581580902400000001 (total 44 digits)
13: 33! - 1 = 8683317618811886495518194401279999999
15: 38! - 1 = 5230226174666011117...4100074291199999999 (total 45 digits)
16: 4137! + 1 = 334525266131638071013763753091226345046...075166515200000000179581580902400000001 (total 5044 digits)
17: 7338! +- 1 = 447011546151268434052302261746660111176...368000000000000000124100074291199999999 (total 10645 digits)
18: 7741! + 1 = 145183092028285869633452526613163807108...800000000000000000140751665152000000001 (total 11450 digits)
19: 9473! -+ 1 = 108736615665674308044701154615126843408...999999999999999999903680000000000000001 (total 147106 digits)
20: 11677! + 1 = 339310868445189820114518309202828586963...000000000000000000148000000000000000001 (total 191114 digits)
21: 15494! +- 1 = 308976961384735088710873661566567430802...000000000000000000199999999999999999999 (total 272147 digits)
22: 166116! -+ 1 = 900369170577843736633931086844518982011...999999999999999999900000000000000000001 (total 298191 digits)
23: 320154! + 1 = 211610334721925248230897696138473508879...000000000000000000100000000000000000001 (total 665272 digits)
24: 324166! - 1 = 228899746017910232190036917057784373664...999999999999999999999999999999999999999 (total 675298 digits)
25: 340320! + 1 = 510086447210371108021161033472192524829...000000000000000000100000000000000000001 (total 715665 digits)
26: 379324! - 1 = 248403074609647070522889974601791023211...999999999999999999999999999999999999999 (total 815675 digits)
27: 399340! + 1 = 160086307116559738151008644721037110809...000000000000000000100000000000000000001 (total 867715 digits)
28: 427379! +- 1 = 290634717696073484124840307460964707050...000000000000000000199999999999999999999 (total 940815 digits)
29: 469399! -+ 1 = 677180966681495109016008630711655973815...999999999999999999900000000000000000001 (total 1051867 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>
 
10,333

edits