Factorial primes: Difference between revisions

m
syntax highlighting fixup automation
(Undo revision 365170 by Chunes (talk) (incorrect))
m (syntax highlighting fixup automation)
Line 30:
=={{header|ALGOL 68}}==
Basic task. Assumes LONG INT is at least 64 bits.
<langsyntaxhighlight lang="algol68">BEGIN # find some factorial primes - primes that are f - 1 or f + 1 #
# for some factorial f #
 
Line 65:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 81:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Factorial primes. Nigel Galloway: August 15th., 2022
let fN g=if Open.Numeric.Primes.MillerRabin.IsProbablePrime &g then Some(g) else None
let fp()=let rec fG n g=seq{let n=n*g in yield (fN(n-1I),-1,g); yield (fN(n+1I),1,g); yield! fG n (g+1I)} in fG 1I 1I|>Seq.filter(fun(n,_,_)->Option.isSome n)
fp()|>Seq.iteri(fun i (n,g,l)->printfn $"""%2d{i+1}: %3d{int l}!%+d{g} -> %s{let n=string(Option.get n) in if n.Length<41 then n else n[0..19]+".."+n[n.Length-20..]+" ["+string(n.Length)+" digits]"}""")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 122:
</pre>
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> (,. (-!)/"1)1>.(,. >.@(!inv)@<:) (#~ 1 p: ]) ~.,(!i.27x)+/1 _1
2 1 1
3 2 1
Line 132:
39916801 11 1
479001599 12 _1
87178291199 14 _1</langsyntaxhighlight>
 
(i.28x here would have given us an eleventh prime, but the task asked for the first 10, and the stretch goal requires considerable patience.)
 
=={{header|Julia}}==
<langsyntaxhighlight lang="ruby">using Primes
 
limitedprint(n) = (s = string(n); n = length(s); return n <= 40 ? s : s[1:20] * "..." * s[end-19:end] * " ($n digits)")
Line 150:
 
showfactorialprimes(1000)
</langsyntaxhighlight>{{out}}
<pre>
1! + 1 -> 2
Line 189:
=={{header|LOLCODE}}==
Basic task, based on the Algol 68 sample.
<langsyntaxhighlight lang="lolcode">OBTW find some factorial primes - primes that are f - 1 or f + 1
for some factorial f
TLDR
Line 257:
 
KTHXBYE
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 273:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 294:
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 332:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub postfix:<!> ($n) { constant @F = (1, 1, |[\*] 2..*); @F[$n] }
sub abr ($_) { .chars < 41 ?? $_ !! .substr(0,20) ~ '..' ~ .substr(*-20) ~ " ({.chars} digits)" }
 
Line 342:
++$limit and printf "%2d: %3d! + 1 = %s\n", $limit, $_, abr f +1 if (f +1).is-prime;
exit if $limit >= 30
}</langsyntaxhighlight>
{{out}}
<pre> 1: 1! + 1 = 2
Line 379:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt
 
Line 395:
i = i + 1
f = f * i
}</langsyntaxhighlight>
 
{{out}}
Line 415:
{{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.
<langsyntaxhighlight lang="ecmascript">import "./gmp" for Mpz
import "./fmt" for Fmt
 
Line 436:
}
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
10,333

edits