Factorial primes: Difference between revisions

Content added Content deleted
(Undo revision 365170 by Chunes (talk) (incorrect))
m (syntax highlighting fixup automation)
Line 30: Line 30:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Basic task. Assumes LONG INT is at least 64 bits.
Basic task. Assumes LONG INT is at least 64 bits.
<lang algol68>BEGIN # find some factorial primes - primes that are f - 1 or f + 1 #
<syntaxhighlight lang="algol68">BEGIN # find some factorial primes - primes that are f - 1 or f + 1 #
# for some factorial f #
# for some factorial f #


Line 65: Line 65:
FI
FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 81: Line 81:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Factorial primes. Nigel Galloway: August 15th., 2022
// Factorial primes. Nigel Galloway: August 15th., 2022
let fN g=if Open.Numeric.Primes.MillerRabin.IsProbablePrime &g then Some(g) else None
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)
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]"}""")
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}}
{{out}}
<pre>
<pre>
Line 122: Line 122:
</pre>
</pre>
=={{header|J}}==
=={{header|J}}==
<lang J> (,. (-!)/"1)1>.(,. >.@(!inv)@<:) (#~ 1 p: ]) ~.,(!i.27x)+/1 _1
<syntaxhighlight lang="j"> (,. (-!)/"1)1>.(,. >.@(!inv)@<:) (#~ 1 p: ]) ~.,(!i.27x)+/1 _1
2 1 1
2 1 1
3 2 1
3 2 1
Line 132: Line 132:
39916801 11 1
39916801 11 1
479001599 12 _1
479001599 12 _1
87178291199 14 _1</lang>
87178291199 14 _1</syntaxhighlight>


(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.)
(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}}==
=={{header|Julia}}==
<lang ruby>using Primes
<syntaxhighlight 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)")
limitedprint(n) = (s = string(n); n = length(s); return n <= 40 ? s : s[1:20] * "..." * s[end-19:end] * " ($n digits)")
Line 150: Line 150:


showfactorialprimes(1000)
showfactorialprimes(1000)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
1! + 1 -> 2
1! + 1 -> 2
Line 189: Line 189:
=={{header|LOLCODE}}==
=={{header|LOLCODE}}==
Basic task, based on the Algol 68 sample.
Basic task, based on the Algol 68 sample.
<lang lolcode>OBTW find some factorial primes - primes that are f - 1 or f + 1
<syntaxhighlight lang="lolcode">OBTW find some factorial primes - primes that are f - 1 or f + 1
for some factorial f
for some factorial f
TLDR
TLDR
Line 257: Line 257:


KTHXBYE
KTHXBYE
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 273: Line 273:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: Line 294:
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 332: Line 332:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>sub postfix:<!> ($n) { constant @F = (1, 1, |[\*] 2..*); @F[$n] }
<syntaxhighlight lang="raku" line>sub postfix:<!> ($n) { constant @F = (1, 1, |[\*] 2..*); @F[$n] }
sub abr ($_) { .chars < 41 ?? $_ !! .substr(0,20) ~ '..' ~ .substr(*-20) ~ " ({.chars} digits)" }
sub abr ($_) { .chars < 41 ?? $_ !! .substr(0,20) ~ '..' ~ .substr(*-20) ~ " ({.chars} digits)" }


Line 342: Line 342:
++$limit and printf "%2d: %3d! + 1 = %s\n", $limit, $_, abr f +1 if (f +1).is-prime;
++$limit and printf "%2d: %3d! + 1 = %s\n", $limit, $_, abr f +1 if (f +1).is-prime;
exit if $limit >= 30
exit if $limit >= 30
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 1: 1! + 1 = 2
<pre> 1: 1! + 1 = 2
Line 379: Line 379:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 395: Line 395:
i = i + 1
i = i + 1
f = f * i
f = f * i
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 415: Line 415:
{{libheader|Wren-gmp}}
{{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.
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.
<lang ecmascript>import "./gmp" for Mpz
<syntaxhighlight lang="ecmascript">import "./gmp" for Mpz
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 436: Line 436:
}
}
i = i + 1
i = i + 1
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}