Fortunate numbers: Difference between revisions
m
syntax highlighting fixup automation
Drkameleon (talk | contribs) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 25:
{{trans|Nim}}
<
I n < 2 | n % 2 == 0
R n == 2
Line 89:
L(m) sorted(Array(s))[0 .< nn]
V i = L.index
print(‘#3’.format(m), end' I (i + 1) % 10 == 0 {"\n"} E ‘ ’)</
{{out}}
Line 103:
=={{header|Arturo}}==
<
primorial: function [n][
product first.n: n firstPrimes
Line 120:
]
print sort fortunates</
{{out}}
Line 128:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
math.ranges prettyprint sequences sets sorting ;
Line 135:
primorial dup next-prime 2dup - abs 1 =
[ next-prime ] when - abs
] map members natural-sort 50 head 10 group simple-table.</
{{out}}
<pre>
Line 149:
Use any primality testing example, the [[set]]s example, and [[Bubble Sort]] as includes for finding primes, removing duplicates, and sorting the output respectively. Coding these up again would bloat the code without being illustrative. Ditto for using a bigint library to get Fortunates after the 7th one, it's just not worth the bother.
<
#include "isprime.bas"
#include "sets.bas"
Line 194:
for n=0 to 6
print forts(n)
next n</
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 242:
}
fmt.Println()
}</
{{out}}
Line 255:
=={{header|Haskell}}==
<
import Math.NumberTheory.Primes.Testing (isPrime)
import Data.List (nub)
Line 268:
fortunateNumbers :: [Integer]
fortunateNumbers = (\p -> nextPrime (p + 2) - p) <$> tail primorials</
<pre>λ> take 50 fortunateNumbers
Line 287:
of fortunate(n) for successive values of n.
<
2, range(3; infinite; 2) | select(is_prime);
Line 303:
if length >= $limit then ., break $out else empty end);
fortunates(10)</
{{out}}
<pre>
Line 311:
=={{header|Julia}}==
<
primorials(N) = accumulate(*, primes(N), init = big"1")
Line 322:
foreach(p -> print(rpad(last(p), 5), first(p) % 10 == 0 ? "\n" : ""),
(map(fortunate, 1:100) |> unique |> sort!)[begin:50] |> enumerate)
</
<pre>
After sorting, the first 50 distinct fortunate numbers are:
Line 333:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
primorials[n_] := Times @@ Prime[Range[n]]
vals = Table[
Line 343:
{i, 100}
];
TakeSmallest[DeleteDuplicates[vals], 50]</
{{out}}
<pre>{3,5,7,13,17,19,23,37,47,59,61,67,71,79,89,101,103,107,109,127,151,157,163,167,191,197,199,223,229,233,239,271,277,283,293,307,311,313,331,353,373,379,383,397,401,409,419,421,439,443}</pre>
Line 350:
{{libheader|bignum}}
Nim doesn’t provide a standard module to deal with big integers. So, we have chosen to use the third party module “bignum” which provides functions to easily find primes and check if a number is prime.
<
import bignum
Line 383:
echo "First $# fortunate numbers:".format(N)
for i, m in list:
stdout.write ($m).align(3), if (i + 1) mod 10 == 0: '\n' else: ' '</
{{out}}
Line 395:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use List::Util <first uniq>;
Line 409:
print "First $upto distinct fortunate numbers:\n" .
(sprintf "@{['%6d' x $upto]}", @fortunate) =~ s/(.{60})/$1\n/gr;</
{{out}}
<pre>First 50 distinct fortunate numbers:
Line 419:
=={{header|Phix}}==
<!--<
<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 438:
<span style="color: #000000;">fortunates</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">fortunates</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first 50 distinct fortunate numbers are:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">fortunates</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 451:
=={{header|Python}}==
{{libheader|sympy}}
<
from sympy.ntheory import isprime
Line 477:
print(('{:<3} ' * 10).format(*(first50[20:30])))
print(('{:<3} ' * 10).format(*(first50[30:40])))
print(('{:<3} ' * 10).format(*(first50[40:])))</
{{out}}
<pre>The first 50 fortunate numbers:
Line 489:
Limit of 75 primorials to get first 50 unique fortunates is arbitrary, found through trial and error.
<syntaxhighlight lang="raku"
say display :title("First 50 distinct fortunate numbers:\n"),
Line 499:
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</
{{out}}
<pre>First 50 distinct fortunate numbers:
Line 511:
For this task's requirement, finding the 8<sup>th</sup> fortunate number requires running this REXX program in a 64-bit address
<br>space. It is CPU intensive as there is no '''isPrime''' BIF for the large (possible) primes generated.
<
numeric digits 12
parse arg n cols . /*obtain optional argument from the CL.*/
Line 564:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</
output
<pre>
Line 577:
=={{header|Ruby}}==
<
primorials = Enumerator.new do |y|
Line 593:
p fortunates[0, limit]
</syntaxhighlight>
{{out}}
<pre>[3, 5, 7, 13, 17, 19, 23, 37, 47, 59, 61, 67, 71, 79, 89, 101, 103, 107, 109, 127, 151, 157, 163, 167, 191, 197, 199, 223, 229, 233, 239, 271, 277, 283, 293, 307, 311, 313, 331, 353, 373, 379, 383, 397, 401, 409, 419, 421, 439, 443]
</pre>
=={{header|Sidef}}==
<
var P = n.pn_primorial
2..Inf -> first {|m| P+m -> is_prob_prime }
Line 617:
say "\n#{limit} Fortunate numbers, sorted with duplicates removed:"
say uniq.sort.first(limit)</
{{out}}
<pre>
Line 633:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
import "/big" for BigInt
import "/sort" for Sort
Line 656:
Sort.quick(fortunates)
System.print("After sorting, the first 50 distinct fortunate numbers are:")
for (chunk in Lst.chunks(fortunates[0..49], 10)) Fmt.print("$3d", chunk)</
{{out}}
|