Factorial primes: Difference between revisions

Add Mathematica/Wolfram Language implementation
(→‎{{header|ALGOL 68}}: Simplified also make it more DRY and less WET)
(Add Mathematica/Wolfram Language implementation)
(9 intermediate revisions by 7 users not shown)
Line 242:
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>
Line 309 ⟶ 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 454 ⟶ 482:
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>
 
 
Line 912 ⟶ 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>
 
Line 1,228 ⟶ 1,416:
872! + 1 = 19723152008295244962...00000000000000000001 (2188 digits)
974! - 1 = 55847687633820181096...99999999999999999999 (2490 digits)
</pre>
 
=={{header|Quackery}}==
 
<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>
 
Line 1,577 ⟶ 1,806:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
Line 1,613 ⟶ 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