Factorial primes: Difference between revisions

Add Mathematica/Wolfram Language implementation
(Factorial primes in FreeBASIC)
(Add Mathematica/Wolfram Language implementation)
(22 intermediate revisions by 16 users not shown)
Line 31:
=={{header|ALGOL 68}}==
Basic task. Assumes LONG INT is at least 64 bits.
<syntaxhighlight lang="algol68">BEGIN # find some factorial primes - primes that are f - 1 or f + 1 #
BEGIN # find some factorial primes - primes that are f - 1 or f + 1 #
# for some factorial f #
 
Line 43 ⟶ 44:
prime
FI;
# end of code based on the primality by trial divisiodivision task #
PROC show factorial prime = ( INT fp number, INT n, CHAR fp op, LONG INT fp )VOID:
print( ( whole( fp number, -2 ), ":", whole( n, -4 )
, "! ", fp op, " 1 = ", whole( fp, 0 )
, newline
)
);
LONG INT f := 1;
INT fp count := 0;
FOR n WHILE fp count < 10 DO
f *:= n;
IFCHAR fp LONG INT fpop := f "- 1";
FOR offset FROM -1 isBY prime(2 fpTO )1 DO
IF LONG INT fp = f + offset;
THEN
show factorial prime( fp countis +:= 1, n, "-",prime( fp )
FI; THEN
IF LONG INT fp = f print( ( whole( fp count +:= 1;, -2 ), ":", whole( n, -4 )
is prime , "! ", fp op, " 1 = ", whole( fp, 0 )
THEN , newline
show factorial prime( fp count +:= 1, n, "+", fp )
FI )
FI;
fp op := "+"
OD
OD
END
END</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
Line 111 ⟶ 110:
9: 12! - 1 = 479001599
10: 14! - 1 = 87178291199</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic">include "isprime.kbs"
include "factorial.kbs"
 
print "First 10 factorial primes:"
found = 0
i = 1
while found < 9
fct = factorial (i)
 
if isprime(fct-1) then
found += 1
print rjust(string(found),2); ": "; rjust(string(i),2); "! - 1 = "; fct-1
end if
if isprime(fct+1) then
found += 1
print rjust(string(found),2); ": "; rjust(string(i),2); "! + 1 = "; fct+1
end if
i += 1
end while
end</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">define found = 0, fct = 0, i = 1
 
do
 
if found < 10 then
 
let fct = factorial(i)
 
if prime(fct - 1) then
 
let found = found + 1
print found, ": ", i, "! - 1 = ", fct - 1
 
endif
 
if prime(fct + 1) then
 
let found = found + 1
print found, ": ", i, "! + 1 = ", fct + 1
 
endif
 
let i = i + 1
 
endif
 
wait
 
loop found < 10</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="vb">#include "isprime.bas"
#include "factorial.bas"
 
Print "First 10 factorial primes:"
Dim As Integer found = 0, i = 1
While found < 10
Dim As Integer fct = factorial (i)
If isprime(fct-1) Then
found += 1
Print Using "##: ##_! - 1 = &"; found; i; fct-1
End If
If isprime(fct+1) Then
found += 1
Print Using "##: ##_! + 1 = &"; found; i; fct+1
End If
i += 1
Wend
Sleep</syntaxhighlight>
{{out}}
<pre>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</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public Sub Main()
Print "First 10 factorial primes:"
Dim found As Long = 0, i As Integer = 1
While found < 10 'más de 10 es ya BigInt
Dim fct As Long = factorial(i)
If isPrime(fct - 1) Then
found += 1
Print Format$(found, "##"); ": "; Format$(i, "##"); "! - 1 = "; fct - 1
End If
If isPrime(fct + 1) Then
found += 1
Print Format$(found, "##"); ": "; Format$(i, "##"); "! + 1 = "; fct + 1
End If
i += 1
Wend
End
 
Public Sub isPrime(ValorEval As Long) As Boolean
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
If ValorEval Mod 3 = 0 Then Return ValorEval = 3
Dim d As Long = 5
While d * d <= ValorEval
If ValorEval Mod d = 0 Then Return False Else d += 2
Wend
Return True
 
End Function
 
Public Function factorial(num As Integer) As Long
Dim result As Long = 1
For i As Integer = 2 To num
result *= i
Next
Return result
End</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="vb">;XIncludeFile "isprime.pb"
;XIncludeFile "factorial.pb"
 
If OpenConsole()
PrintN("First 10 factorial primes:")
Define found.i = 0, i,i = 1, fct.i
While found < 10
fct = factorial (i)
If isprime(fct-1)
found + 1
PrintN(RSet(Str(found),2) + ": " + RSet(Str(i),2) + "! - 1 = " + Str(fct-1))
EndIf
If isprime(fct+1)
found + 1
PrintN(RSet(Str(found),2) + ": " + RSet(Str(i),2) + "! + 1 = " + Str(fct+1))
EndIf
i + 1
Wend
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()
EndIf</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="vb">function isPrime(n)
if n < 2 then isPrime = 0 : goto [exit]
if n = 2 then isPrime = 1 : goto [exit]
if n mod 2 = 0 then isPrime = 0 : goto [exit]
isPrime = 1
for i = 3 to int(n^.5) step 2
if n mod i = 0 then isPrime = 0 : goto [exit]
next i
[exit]
end function
 
function factorial(n)
factorial = 1
if n > 1 then factorial = n * factorial(n -1)
end function
 
print "First 10 factorial primes:"
found = 0
i = 1
while found < 10
fct = factorial(i)
 
if isPrime(fct-1) then
found = found + 1
print using("##", found); ": "; using("##", i); "! - 1 = "; fct-1
end if
if isPrime(fct+1) then
found = found + 1
print using("##", found); ": "; using("##", i); "! + 1 = "; fct+1
end if
i = i + 1
wend</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic">import isprime
import factorial
 
print "First 10 factorial primes:"
found = 0
i = 1
while found < 10
fct = factorial (i)
if isPrime(fct-1) then
found = found + 1
print found using("##"), ": ", i using("##"), "! - 1 = ", fct-1
fi
if isPrime(fct+1) then
found = found + 1
print found using("##"), ": ", i using("##"), "! + 1 = ", fct+1
fi
i = i + 1
end while</syntaxhighlight>
 
=={{header|C++}}==
Line 120 ⟶ 337:
using big_int = mpz_class;
 
std::string to_string(const big_int& num, size_t nmax_digits) {
std::string str = num.get_str();
size_t len = str.size();
if (len > nmax_digits) {
str = str.substrreplace(0, nmax_digits / 2), +len - max_digits, "..." + str.substr(len - n / 2);
str += " (";
str += std::to_string(len);
Line 187 ⟶ 404:
31: 546! - 1 = 14130200926141832545...99999999999999999999 (1260 digits)
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: int64): boolean;
{Optimised prime test - about 40% faster than the naive approach}
var I,Stop: integer;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N*1.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (i + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
function Factorial(N: Word): int64;
var I: integer;
begin
Result:= 1;
for I := 2 to N do Result:=Result * I;
end;
 
 
procedure ShowFactorialPrimes(Memo: TMemo);
{Show factorials where F+1 or F-1 are prime}
var I,Cnt: integer;
var F: int64;
 
procedure DisplayItem(Minus: boolean);
var S: string;
var Sign: char;
var F1: int64;
begin
Inc(Cnt);
if Minus then F1:=F-1 else F1:=F+1;
if Minus then Sign:='-' else Sign:='+';
S:=Format('%2d: %3d! %s 1 = %d',[Cnt,I,Sign,F1]);
Memo.Lines.Add(S);
end;
 
begin
Cnt:=0;
for I:=1 to High(Integer) do
begin
F:=Factorial(I);
if IsPrime(F+1) then DisplayItem(False);
if IsPrime(F-1) then DisplayItem(True);
if Cnt>=10 then break;
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
1: 1! + 1 = 2
2: 2! + 1 = 3
3: 3! + 1 = 7
4: 3! - 1 = 5
5: 4! - 1 = 23
6: 6! - 1 = 719
7: 7! - 1 = 5039
8: 11! + 1 = 39916801
9: 12! - 1 = 479001599
10: 14! - 1 = 87178291199
</pre>
 
 
=={{header|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight>
func isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
f = 1
while count < 10
n += 1
f *= n
op$ = "-"
for fp in [ f - 1 f + 1 ]
if isprim fp = 1
count += 1
print n & "! " & op$ & " 1 = " & fp
.
op$ = "+"
.
.
</syntaxhighlight>
 
 
=={{header|F_Sharp|F#}}==
Line 230 ⟶ 558:
</pre>
 
=={{header|FreeBASICFutureBasic}}==
<syntaxhighlight lang="vbfuturebasic">#include "isprime.bas"
#include "factorialNSLog.basincl"
 
local fn Factorial( n as NSUInteger ) as NSUInteger
NSUInteger factorial = 1
if n > 1 then factorial = n * fn Factorial( n -1 )
end fn = factorial
 
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
void local fn FactorialPrimes( n as long )
NSUInteger found = 0, i = 1
NSLog( @"First %lu factorial primes:", n )
while ( found < n )
NSUInteger fct = fn Factorial( i )
if ( fn IsPrime( fct - 1 ) )
found++
NSLog( @"%2lu: %3lu! - 1 = %-lu", found, i, fct - 1 )
end if
if ( fn IsPrime( fct + 1 ) )
found++
NSLog( @"%2lu: %3lu! + 1 = %-lu", found, i, fct + 1 )
end if
i++
wend
end fn
 
fn FactorialPrimes( 10 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
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
</pre>
 
Print "First 10 factorial primes:"
Dim As Integer found = 0, i = 1
While found < 10
Dim As Integer fct = factorial (i)
If isprime(fct-1) Then
found += 1
Print Using "##: ##_! - 1 = &"; found; i; fct-1
End If
If isprime(fct+1) Then
found += 1
Print Using "##: ##_! + 1 = &"; found; i; fct+1
End If
i += 1
Wend
Sleep</syntaxhighlight>
{{out}}
<pre>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</pre>
 
=={{header|J}}==
Line 617 ⟶ 972:
9: 12! - 1 = 479001599
10: 14! - 1 = 87178291199
</pre>
 
=={{header|Lua}}==
{{Trans|ALGOL 68}}
<syntaxhighlight lang="lua">
do -- find some factorial primes - primes that are f - 1 or f + 1
-- for some factorial f
 
function isPrime( p )
if p <= 1 or p % 2 == 0 then
return p == 2
else
local prime = true
local i = 3
local rootP = math.floor( math.sqrt( p ) )
while i <= rootP and prime do
prime = p % i ~= 0
i = i + 2
end
return prime
end
end
 
local f = 1
local fpCount = 0
local n = 0
local fpOp = ""
while fpCount < 10 do
n = n + 1
f = f * n
fpOp = "-"
for fp = f - 1, f + 1, 2 do
if isPrime( fp ) then
fpCount = fpCount + 1
io.write( string.format( "%2d", fpCount ), ":"
, string.format( "%4d", n ), "! "
, fpOp, " 1 = ", fp, "\n"
)
end
fpOp = "+"
end
end
 
end
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{trans|Julia}}
<syntaxhighlight lang="Mathematica">
LimitedPrint[n_] := Module[{s = IntegerString[n], len},
len = StringLength[s];
If[len <= 40, s, StringJoin[StringTake[s, 20], "...", StringTake[s, -20], " (", ToString[len], " digits)"]]
]
 
ShowFactorialPrimes[N_] := Module[{f},
Do[
f = Factorial[i];
If[PrimeQ[f - 1], Print[IntegerString[i, 10, 3], "! - 1 -> ", LimitedPrint[f - 1]]];
If[PrimeQ[f + 1], Print[IntegerString[i, 10, 3], "! + 1 -> ", LimitedPrint[f + 1]]],
{i, 1, N}
]
]
 
ShowFactorialPrimes[1000]
</syntaxhighlight>
{{out}}
<pre>
001! + 1 -> 2
002! + 1 -> 3
003! - 1 -> 5
003! + 1 -> 7
004! - 1 -> 23
006! - 1 -> 719
007! - 1 -> 5039
011! + 1 -> 39916801
012! - 1 -> 479001599
014! - 1 -> 87178291199
027! + 1 -> 10888869450418352160768000001
030! - 1 -> 265252859812191058636308479999999
032! - 1 -> 263130836933693530167218012159999999
033! - 1 -> 8683317618811886495518194401279999999
037! + 1 -> 13763753091226345046...79581580902400000001 (44 digits)
038! - 1 -> 52302261746660111176...24100074291199999999 (45 digits)
041! + 1 -> 33452526613163807108...40751665152000000001 (50 digits)
073! + 1 -> 44701154615126843408...03680000000000000001 (106 digits)
077! + 1 -> 14518309202828586963...48000000000000000001 (114 digits)
094! - 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>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
block([i:1,count:0,result:[]],
while count<10 do (if primep(i!-1) or primep(i!+1) then (result:endcons(i,result),count:count+1),i:i+1),
result:map(lambda([x],[x,x!-1,x!+1]),result),
append(map(lambda([x],if primep(x[2]) then [x[1],x[2],"subtracted"]),result),map(lambda([x],if primep(x[3]) then [x[1],x[3],"added"]),result)),
unique(%%),
firstn(%%,10);
</syntaxhighlight>
{{out}}
<pre>
[[1,2,"added"],[2,3,"added"],[3,5,"subtracted"],[3,7,"added"],[4,23,"subtracted"],[6,719,"subtracted"],[7,5039,"subtracted"],[11,39916801,"added"],[12,479001599,"subtracted"],[14,87178291199,"subtracted"]]
</pre>
 
=={{header|Nim}}==
{{libheader|Nim-Integers}}
Nim standard integer types are limited to 64 bits. So we use an external library which provides arbitrary sized integers.
 
<syntaxhighlight lang="nim">import std/[math, strformat]
 
# Task.
 
func isPrime(n: int): bool =
if n < 2: return false
if n == 2 or n == 3: return true
if n mod 2 == 0: return false
if n mod 3 == 0: return false
var d = 5
var step = 2
while d * d <= n:
if n mod d == 0:
return false
inc d, step
step = 6 - step
return true
 
echo "First 10 factorial primes:\n"
var count = 0
var n = 1
while count < 10:
let f = fac(n)
if isPrime(f - 1):
inc count
echo &"{count:>2}: {n:>3}! - 1 = {f - 1}"
if count < 10 and isPrime(f + 1):
inc count
echo &"{count:>2}: {n:>3}! + 1 = {f + 1}"
inc n
 
 
# Stretch.
 
import integers
 
func str(n: Integer): string =
## Return the string representation of an Integer.
result = $n
if result.len > 40:
result = &"{result[0..19]}...{result[^20..^1]} ({result.len} digits)"
 
echo "\n\nNext 20 factorial primes:\n"
while count < 30:
let f: Integer = factorial(n)
if isPrime(f - 1):
inc count
echo &"{count:>2}: {n:>3}! - 1 = {str(f - 1)}"
if isPrime(f + 1):
inc count
echo &"{count:>2}: {n:>3}! - 1 = {str(f + 1)}"
inc n
</syntaxhighlight>
{{out}}
<pre>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
 
 
Next 20 factorial primes:
 
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)
</pre>
 
Line 839 ⟶ 1,418:
</pre>
 
=={{header|RacketQuackery}}==
{{incorrect|Racket|"As 0! (by convention) and 1! are both 1, ignore the former and start counting from 1!", (not 2!).}}
 
<code>!</code> is defined at [[Factorial#Quackery]].
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ dup 10 < if sp echo ] is recho ( n --> )
 
[] 0
[ 1+ dup !
dup dip
[ 1 - isprime if
[ tuck negate join swap ] ]
1+ isprime if
[ tuck join swap ]
over size 9 > until ]
drop 10 split drop
witheach
[ i^ 1+
recho say ": "
dup abs tuck recho
0 < iff
[ say "! - 1 = " -1 ]
else
[ say "! + 1 = " 1 ]
swap ! + echo cr ]</syntaxhighlight>
 
{{out}}
 
<pre> 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
</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
Line 851 ⟶ 1,469:
(stream-cons `(+ ,n ,(add1 curr-fact))
(factorial-stream-iter (add1 n) (* curr-fact (+ n 1))))))
(factorial-stream-iter 21 21))
 
(define (format-large-number n)
Line 884 ⟶ 1,502:
 
<pre>
1: 21! + 1 = 32
2: 32! -+ 1 = 53
3: 3! +- 1 = 75
4: 43! -+ 1 = 237
5: 64! - 1 = 71923
6: 76! - 1 = 5039719
7: 117! +- 1 = 399168015039
8: 1211! -+ 1 = 47900159939916801
9: 1412! - 1 = 87178291199479001599
10: 2714! +- 1 = 1088886945041835216076800000187178291199
11: 3027! -+ 1 = 26525285981219105863630847999999910888869450418352160768000001
12: 3230! - 1 = 263130836933693530167218012159999999265252859812191058636308479999999
13: 3332! - 1 = 8683317618811886495518194401279999999263130836933693530167218012159999999
14: 33! - 1 = 8683317618811886495518194401279999999
14: 37! + 1 = 1376375309122634504...9581580902400000001 (total 44 digits)
15: 3837! -+ 1 = 52302261746660111171376375309122634504...41000742911999999999581580902400000001 (total 4544 digits)
16: 4138! +- 1 = 33452526613163807105230226174666011117...07516651520000000014100074291199999999 (total 5045 digits)
17: 7341! + 1 = 44701154615126843403345252661316380710...36800000000000000010751665152000000001 (total 10650 digits)
18: 7773! + 1 = 14518309202828586964470115461512684340...80000000000000000013680000000000000001 (total 114106 digits)
19: 9477! -+ 1 = 10873661566567430801451830920282858696...99999999999999999998000000000000000001 (total 147114 digits)
20: 11694! +- 1 = 33931086844518982011087366156656743080...00000000000000000019999999999999999999 (total 191147 digits)
21: 154116! + 1 = 30897696138473508873393108684451898201...0000000000000000001 (total 272191 digits)
22: 166154! -+ 1 = 90036917057784373663089769613847350887...99999999999999999990000000000000000001 (total 298272 digits)
23: 320166! +- 1 = 21161033472192524829003691705778437366...00000000000000000019999999999999999999 (total 665298 digits)
24: 324320! -+ 1 = 22889974601791023212116103347219252482...99999999999999999990000000000000000001 (total 675665 digits)
25: 340324! +- 1 = 51008644721037110802288997460179102321...00000000000000000019999999999999999999 (total 715675 digits)
26: 379340! -+ 1 = 24840307460964707055100864472103711080...99999999999999999990000000000000000001 (total 815715 digits)
27: 399379! +- 1 = 16008630711655973812484030746096470705...00000000000000000019999999999999999999 (total 867815 digits)
28: 427399! + 1 = 29063471769607348411600863071165597381...0000000000000000001 (total 940867 digits)
29: 469427! -+ 1 = 67718096668149510902906347176960734841...99999999999999999990000000000000000001 (total 1051940 digits)
30: 546469! - 1 = 14130200926141832546771809666814951090...9999999999999999999 (total 12601051 digits)
cpu time: 2440 real time: 2440 gc time: 3</pre>
 
Line 959 ⟶ 1,577:
29: 427! + 1 = 29063471769607348411..00000000000000000001 (940 digits)
30: 469! - 1 = 67718096668149510900..99999999999999999999 (1051 digits)</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
see "working..." + nl
load "stdlibcore.ring"
 
n = 0
num = 0
while true
n++
n1 = factorial(n) - 1
if isPrime(n1)
num++
see "" + num + ": " + n + "! - 1 = " + n1 + nl
ok
n2 = factorial(n) + 1
if isPrime(n2)
num++
see "" + num + ": " + n + "! + 1 = " + n2 + nl
ok
if num = 10
exit
ok
end
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|RPL}}==
Line 1,053 ⟶ 1,710:
469! - 1 = 67718096668149510900...99999999999999999999
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var factorial_primes = Enumerator({|f|
for k in (1..Inf) {
if (k!-1 -> is_prime) { f([k, -1]) }
if (k!+1 -> is_prime) { f([k, +1]) }
}
})
 
func abr(v) {
v.len <= 40 ? v : (v.to_s.first(20) + '..' + v.to_s.last(20) + " (#{v.len} digits)")
}
 
factorial_primes.first(30).each_2d {|k,i|
printf("%3d! %s %d = %s\n", k, (i.sgn < 0 ? '-' : '+'), i.abs, abr(k! + i))
}</syntaxhighlight>
{{out}}
<pre> 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
27! + 1 = 10888869450418352160768000001
30! - 1 = 265252859812191058636308479999999
32! - 1 = 263130836933693530167218012159999999
33! - 1 = 8683317618811886495518194401279999999
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)</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
import math
 
fn main() {
mut n, mut count := 0, 0
for count < 10 {
n++
f := math.factoriali(n)
if is_prime(f - 1) {
count++
println("${count}: ${n}! - 1 = ${f - 1}")
}
if is_prime(f + 1) {
count++
println("${count}: ${n}! + 1 = ${f + 1}")
}
}
}
 
fn is_prime(num i64) bool {
if num <= 1 {return false}
if num % 2 == 0 && num != 2 {return false}
for idx := 3; idx <= math.floor(num / 2) - 1; idx += 2 {
if num % idx == 0 {return false}
}
return true
}
</syntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
=={{header|Wren}}==
===Basic===
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
Line 1,093 ⟶ 1,842:
{{libheader|Wren-gmp}}
This takes about 28.5 seconds to reach the 33rd factorial prime on my machine (Core i7) with the last two being noticeably slower to emerge. Likely to be very slow after that as the next factorial prime is 1477! + 1.
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpz
import "./fmt" for Fmt
 
337

edits