Factorial primes

From Rosetta Code
Revision as of 13:41, 12 August 2022 by PureFox (talk | contribs) (Created new draft task and added a Wren solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Factorial primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definition

A factorial prime is a prime number that is one less or one more than a factorial.

In other words a non-negative integer n corresponds to a factorial prime if either n! - 1 or n! + 1 is prime.

Examples

4 corresponds to the factorial prime 4! - 1 = 23.

5 doesn't correspond to a factorial prime because neither 5! - 1 = 119 (7 x 17) nor 5! + 1 = 121 (11 x 11) are prime.

Task

Find and show here the first 10 factorial primes. As well as the prime itself show the factorial number n to which it corresponds and whether 1 is to be added or subtracted.

As 0! (by convention) and 1! are both 1, ignore the former and start counting from 1!.

Stretch

If your language supports arbitrary sized integers, do the same for at least the next 19 factorial primes.

As it can take a long time to demonstrate that a large number (above say 2^64) is definitely prime, you may instead use a function which shows that a number is probably prime to a reasonable degree of certainty. Most 'big integer' libraries have such a function.

If a number has more than 40 digits, do not show the full number. Show instead the first 20 and the last 20 digits and how many digits in total the number has.

Reference



Wren

Basic

Library: Wren-math
Library: Wren-fmt

<lang ecmascript>import "./math" for Int import "./fmt" for Fmt

System.print("First 10 factorial primes;") var c = 0 var i = 1 while (true) {

   var f = Int.factorial(i)
   if (Int.isPrime(f-1)) {
       Fmt.print("$2d: $2d! - 1 = $d", c = c + 1, i, f - 1)
       if (c == 10) return
   }
   if (Int.isPrime(f+1)) {
       Fmt.print("$2d: $2d! + 1 = $d", c = c + 1, i, f + 1)
       if (c == 10) return
   }
   i = i + 1

}</lang>

Output:
First 10 factorial primes;
 1:  1! + 1 = 2
 2:  2! + 1 = 3
 3:  3! - 1 = 5
 4:  3! + 1 = 7
 5:  4! - 1 = 23
 6:  6! - 1 = 719
 7:  7! - 1 = 5039
 8: 11! + 1 = 39916801
 9: 12! - 1 = 479001599
10: 14! - 1 = 87178291199

Stretch

Library: Wren-gmp

This takes about 28.5 seconds to reach the 33rd factorial prime on my machine (Core i7) with the last two being noticably slower to emerge. Likely to be very slow after that as the next factorial prime is 1477! + 1. <lang ecmascript>import "./gmp" for Mpz import "./fmt" for Fmt

var limit = 33 var c = 0 var i = 1 var f = Mpz.one System.print("First %(limit) factorial primes;")

while (true) {

   f.mul(i)
   var r = (i < 21) ? 1 : 0  // test for definite primeness below 2^64
   if ((f-1).probPrime(15) > r) {
       var s = (f-1).toString
       var sc = s.count
       var digs = sc > 40 ? "(%(sc) digits)" : ""
       Fmt.print("$2d: $3d! - 1 = $20a $s", c = c + 1, i, s, digs)
       if (c == limit) return
   }
   if ((f+1).probPrime(15) > r) {
       var s = (f+1).toString
       var sc = s.count
       var digs = sc > 40 ? "(%(sc) digits)" : ""
       Fmt.print("$2d: $3d! + 1 = $20a $s", c = c + 1, i, s, digs)
       if (c == limit) return
   }
   i = i + 1

}</lang>

Output:
First 33 factorial primes;
 1:   1! + 1 = 2  
 2:   2! + 1 = 3  
 3:   3! - 1 = 5  
 4:   3! + 1 = 7  
 5:   4! - 1 = 23  
 6:   6! - 1 = 719  
 7:   7! - 1 = 5039  
 8:  11! + 1 = 39916801  
 9:  12! - 1 = 479001599  
10:  14! - 1 = 87178291199  
11:  27! + 1 = 10888869450418352160768000001  
12:  30! - 1 = 265252859812191058636308479999999  
13:  32! - 1 = 263130836933693530167218012159999999  
14:  33! - 1 = 8683317618811886495518194401279999999  
15:  37! + 1 = 13763753091226345046...79581580902400000001 (44 digits)
16:  38! - 1 = 52302261746660111176...24100074291199999999 (45 digits)
17:  41! + 1 = 33452526613163807108...40751665152000000001 (50 digits)
18:  73! + 1 = 44701154615126843408...03680000000000000001 (106 digits)
19:  77! + 1 = 14518309202828586963...48000000000000000001 (114 digits)
20:  94! - 1 = 10873661566567430802...99999999999999999999 (147 digits)
21: 116! + 1 = 33931086844518982011...00000000000000000001 (191 digits)
22: 154! + 1 = 30897696138473508879...00000000000000000001 (272 digits)
23: 166! - 1 = 90036917057784373664...99999999999999999999 (298 digits)
24: 320! + 1 = 21161033472192524829...00000000000000000001 (665 digits)
25: 324! - 1 = 22889974601791023211...99999999999999999999 (675 digits)
26: 340! + 1 = 51008644721037110809...00000000000000000001 (715 digits)
27: 379! - 1 = 24840307460964707050...99999999999999999999 (815 digits)
28: 399! + 1 = 16008630711655973815...00000000000000000001 (867 digits)
29: 427! + 1 = 29063471769607348411...00000000000000000001 (940 digits)
30: 469! - 1 = 67718096668149510900...99999999999999999999 (1051 digits)
31: 546! - 1 = 14130200926141832545...99999999999999999999 (1260 digits)
32: 872! + 1 = 19723152008295244962...00000000000000000001 (2188 digits)
33: 974! - 1 = 55847687633820181096...99999999999999999999 (2490 digits)