Jump to content

Fortunate numbers: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 25:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F isProbablePrime(n, k = 10)
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 ‘ ’)</langsyntaxhighlight>
 
{{out}}
Line 103:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">firstPrimes: select 1..100 => prime?
primorial: function [n][
product first.n: n firstPrimes
Line 120:
]
 
print sort fortunates</langsyntaxhighlight>
 
{{out}}
Line 128:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: grouping io kernel math math.factorials math.primes
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.</langsyntaxhighlight>
{{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.
 
<langsyntaxhighlight lang="freebasic">
#include "isprime.bas"
#include "sets.bas"
Line 194:
for n=0 to 6
print forts(n)
next n</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 242:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 255:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes (primes)
import Math.NumberTheory.Primes.Testing (isPrime)
import Data.List (nub)
Line 268:
 
fortunateNumbers :: [Integer]
fortunateNumbers = (\p -> nextPrime (p + 2) - p) <$> tail primorials</langsyntaxhighlight>
 
<pre>λ> take 50 fortunateNumbers
Line 287:
of fortunate(n) for successive values of n.
 
<langsyntaxhighlight lang="jq">def primes:
2, range(3; infinite; 2) | select(is_prime);
Line 303:
if length >= $limit then ., break $out else empty end);
 
fortunates(10)</langsyntaxhighlight>
{{out}}
<pre>
Line 311:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
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)
</langsyntaxhighlight>{{out}}
<pre>
After sorting, the first 50 distinct fortunate numbers are:
Line 333:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[primorials]
primorials[n_] := Times @@ Prime[Range[n]]
vals = Table[
Line 343:
{i, 100}
];
TakeSmallest[DeleteDuplicates[vals], 50]</langsyntaxhighlight>
{{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.
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strutils
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: ' '</langsyntaxhighlight>
 
{{out}}
Line 395:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
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;</langsyntaxhighlight>
{{out}}
<pre>First 50 distinct fortunate numbers:
Line 419:
 
=={{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 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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 451:
=={{header|Python}}==
{{libheader|sympy}}
<langsyntaxhighlight lang="python">from sympy.ntheory.generate import primorial
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:])))</langsyntaxhighlight>
{{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" perl6line>my @primorials = [\*] grep *.is-prime, ^∞;
 
say display :title("First 50 distinct fortunate numbers:\n"),
Line 499:
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</langsyntaxhighlight>
{{out}}
<pre>First 50 distinct fortunate numbers:
Line 511:
For this task's requirement, &nbsp; finding the 8<sup>th</sup> fortunate number requires running this REXX program in a 64-bit address
<br>space. &nbsp; It is CPU intensive as there is no &nbsp; '''isPrime''' &nbsp; BIF for the large (possible) primes generated.
<langsyntaxhighlight lang="rexx">/*REXX program finds/displays fortunate numbers N, where N is specified (default=8).*/
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</langsyntaxhighlight>
output
<pre>
Line 577:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "gmp"
 
primorials = Enumerator.new do |y|
Line 593:
p fortunates[0, limit]
</syntaxhighlight>
</lang>
{{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}}==
<langsyntaxhighlight lang="ruby">func fortunate(n) {
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)</langsyntaxhighlight>
{{out}}
<pre>
Line 633:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
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)</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.