Factorial primes: Difference between revisions
Content added Content deleted
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: | 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| |
=={{header|Python}}== |
||
{{libheader|gmpy2}} |
|||
⚫ | |||
#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)) |
|||
⚫ | |||
(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)) |
|||
⚫ | |||
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]: |
|||
⚫ | |||
fact = 1 |
|||
(stream-filter (λ (l) (prime? (third l))) (factorial-boundary-stream)) 14))] |
|||
for i in count(1): |
|||
⚫ | |||
(begin |
|||
⚫ | |||
(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): |
|||
⚫ | |||
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 |
|||
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 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
14: 37! + 1 = 1376375309122634504...9581580902400000001 (total 44 digits) |
|||
⚫ | |||
15: 38! - 1 = 5230226174666011117...4100074291199999999 (total 45 digits) |
|||
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> |
</pre> |
||