Factorial primes: Difference between revisions

Content added Content deleted
m (Undo revision 338724 by Dr-neptune (talk) Erroneously overwrote Python)
Tag: Undo
m (→‎{{header|Racket}}: Re-add Racket example after restoring Python)
Line 805: Line 805:
974! - 1 = 55847687633820181096...99999999999999999999 (2490 digits)
974! - 1 = 55847687633820181096...99999999999999999999 (2490 digits)
</pre>
</pre>

=={{header|Racket}}==

<syntaxhighlight lang="racket">
#lang racket
(require gmp)

(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)
(let* ([num-chars (number->string n)]
[num-len (string-length num-chars)])
(if (> num-len 40)
(string-append
(substring num-chars 0 19)
"..."
(substring num-chars (- num-len 19) num-len)
(format " (total ~a digits)" num-len))
n)))

(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)))))

(define (prime? n)
(not (zero? (mpz_probab_prime_p (mpz n) 10))))

(for ([i (in-stream
(stream-take
(stream-filter (λ (l) (prime? (third l))) (factorial-boundary-stream)) 30))]
[n (in-naturals 1)])
(begin
(display (format "~a:\t" n))
(factorial-printer i)))

;; time output of above code: 2.46 seconds
</syntaxhighlight>

<pre>
1: 2! + 1 = 3
2: 3! - 1 = 5
3: 3! + 1 = 7
4: 4! - 1 = 23
5: 6! - 1 = 719
6: 7! - 1 = 5039
7: 11! + 1 = 39916801
8: 12! - 1 = 479001599
9: 14! - 1 = 87178291199
10: 27! + 1 = 10888869450418352160768000001
11: 30! - 1 = 265252859812191058636308479999999
12: 32! - 1 = 263130836933693530167218012159999999
13: 33! - 1 = 8683317618811886495518194401279999999
14: 37! + 1 = 1376375309122634504...9581580902400000001 (total 44 digits)
15: 38! - 1 = 5230226174666011117...4100074291199999999 (total 45 digits)
16: 41! + 1 = 3345252661316380710...0751665152000000001 (total 50 digits)
17: 73! + 1 = 4470115461512684340...3680000000000000001 (total 106 digits)
18: 77! + 1 = 1451830920282858696...8000000000000000001 (total 114 digits)
19: 94! - 1 = 1087366156656743080...9999999999999999999 (total 147 digits)
20: 116! + 1 = 3393108684451898201...0000000000000000001 (total 191 digits)
21: 154! + 1 = 3089769613847350887...0000000000000000001 (total 272 digits)
22: 166! - 1 = 9003691705778437366...9999999999999999999 (total 298 digits)
23: 320! + 1 = 2116103347219252482...0000000000000000001 (total 665 digits)
24: 324! - 1 = 2288997460179102321...9999999999999999999 (total 675 digits)
25: 340! + 1 = 5100864472103711080...0000000000000000001 (total 715 digits)
26: 379! - 1 = 2484030746096470705...9999999999999999999 (total 815 digits)
27: 399! + 1 = 1600863071165597381...0000000000000000001 (total 867 digits)
28: 427! + 1 = 2906347176960734841...0000000000000000001 (total 940 digits)
29: 469! - 1 = 6771809666814951090...9999999999999999999 (total 1051 digits)
30: 546! - 1 = 1413020092614183254...9999999999999999999 (total 1260 digits)
cpu time: 2440 real time: 2440 gc time: 3</pre>


=={{header|Raku}}==
=={{header|Raku}}==