Unprimeable numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: note use of 'ntheory' module)
m (syntax highlighting fixup automation)
Line 55: Line 55:
{{trans|Python}}
{{trans|Python}}


<lang 11l>V limit = 10'000'000
<syntaxhighlight lang="11l">V limit = 10'000'000
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 102: Line 102:


L(v) first
L(v) first
print(L.index‘ ending: ’v)</lang>
print(L.index‘ ending: ’v)</syntaxhighlight>


{{out}}
{{out}}
Line 127: Line 127:
If running this with Algol 68G under Windows (and possibly other platforms) you will need to increase the heap size by specifying e.g. <code>-heap 256M</code> on the command line.
If running this with Algol 68G under Windows (and possibly other platforms) you will need to increase the heap size by specifying e.g. <code>-heap 256M</code> on the command line.
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find unprimable numbers - numbers which can't be made into a prime by changing one digit #
<syntaxhighlight lang="algol68">BEGIN # find unprimable numbers - numbers which can't be made into a prime by changing one digit #
# construct a sieve of primes up to max prime #
# construct a sieve of primes up to max prime #
PR read "primes.incl.a68" PR
PR read "primes.incl.a68" PR
Line 226: Line 226:
)
)
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 245: Line 245:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>unprimeable?: function [n][
<syntaxhighlight lang="rebol">unprimeable?: function [n][
if prime? n -> return false
if prime? n -> return false
nd: to :string n
nd: to :string n
Line 274: Line 274:
print first.n: 35 unprimeables
print first.n: 35 unprimeables
print ""
print ""
print ["600th unprimeable number:" last unprimeables]</lang>
print ["600th unprimeable number:" last unprimeables]</syntaxhighlight>


{{out}}
{{out}}
Line 285: Line 285:
=={{header|C}}==
=={{header|C}}==
{{trans|C++}}
{{trans|C++}}
<lang c>#include <assert.h>
<syntaxhighlight lang="c">#include <assert.h>
#include <locale.h>
#include <locale.h>
#include <stdbool.h>
#include <stdbool.h>
Line 425: Line 425:
printf("Least unprimeable number ending in %u: %'u\n" , i, lowest[i]);
printf("Least unprimeable number ending in %u: %'u\n" , i, lowest[i]);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 445: Line 445:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
#include <cstdint>
#include "prime_sieve.hpp"
#include "prime_sieve.hpp"
Line 514: Line 514:
std::cout << "Least unprimeable number ending in " << i << ": " << lowest[i] << '\n';
std::cout << "Least unprimeable number ending in " << i << ": " << lowest[i] << '\n';
return 0;
return 0;
}</lang>
}</syntaxhighlight>


Contents of prime_sieve.hpp:
Contents of prime_sieve.hpp:
<lang cpp>#ifndef PRIME_SIEVE_HPP
<syntaxhighlight lang="cpp">#ifndef PRIME_SIEVE_HPP
#define PRIME_SIEVE_HPP
#define PRIME_SIEVE_HPP


Line 568: Line 568:
}
}


#endif</lang>
#endif</syntaxhighlight>


{{out}}
{{out}}
Line 589: Line 589:
=={{header|D}}==
=={{header|D}}==
{{trans|Java}}
{{trans|Java}}
<lang d>import std.algorithm;
<syntaxhighlight lang="d">import std.algorithm;
import std.array;
import std.array;
import std.conv;
import std.conv;
Line 688: Line 688:
writefln(" %d is %,d", i, v);
writefln(" %d is %,d", i, v);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 35 unprimeable numbers:
<pre>First 35 unprimeable numbers:
Line 713: Line 713:
===The function===
===The function===
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Unprimeable numbers. Nigel Galloway: May 4th., 2021
// Unprimeable numbers. Nigel Galloway: May 4th., 2021
let rec fN i g e l=seq{yield! [0..9]|>Seq.map(fun n->n*g+e+l); if g>1 then let g=g/10 in yield! fN(i+g*(e/g)) g (e%g) i}
let rec fN i g e l=seq{yield! [0..9]|>Seq.map(fun n->n*g+e+l); if g>1 then let g=g/10 in yield! fN(i+g*(e/g)) g (e%g) i}
let fG(n,g)=fN(n*(g/n)) n (g%n) 0|>Seq.exists(isPrime)
let fG(n,g)=fN(n*(g/n)) n (g%n) 0|>Seq.exists(isPrime)
let uP()=let rec fN n g=seq{yield! {n..g-1}|>Seq.map(fun g->(n,g)); yield! fN(g)(g*10)} in fN 1 10|>Seq.filter(fG>>not)|>Seq.map snd
let uP()=let rec fN n g=seq{yield! {n..g-1}|>Seq.map(fun g->(n,g)); yield! fN(g)(g*10)} in fN 1 10|>Seq.filter(fG>>not)|>Seq.map snd
</syntaxhighlight>
</lang>
===The Task===
===The Task===
<lang fsharp>
<syntaxhighlight lang="fsharp">
uP()|>Seq.take 35|>Seq.iter(printf "%d "); printfn ""
uP()|>Seq.take 35|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
200 204 206 208 320 322 324 325 326 328 510 512 514 515 516 518 530 532 534 535 536 538 620 622 624 625 626 628 840 842 844 845 846 848 890
200 204 206 208 320 322 324 325 326 328 510 512 514 515 516 518 530 532 534 535 536 538 620 622 624 625 626 628 840 842 844 845 846 848 890
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
printfn "600th unprimable number is %d" (uP()|>Seq.item 599)
printfn "600th unprimable number is %d" (uP()|>Seq.item 599)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
600th unprimable number is 5242
600th unprimable number is 5242
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
[0..9]|>Seq.iter(fun n->printfn "first umprimable number ending in %d is %d" n (uP()|>Seq.find(fun g->n=g%10)))
[0..9]|>Seq.iter(fun n->printfn "first umprimable number ending in %d is %d" n (uP()|>Seq.find(fun g->n=g%10)))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 753: Line 753:
===Optimized for optional part of task===
===Optimized for optional part of task===
The above general implementation can complete all of the task but takes over 1min for the optional part. The following completes the optional part in 3secs.
The above general implementation can complete all of the task but takes over 1min for the optional part. The following completes the optional part in 3secs.
<lang fsharp>
<syntaxhighlight lang="fsharp">
let uPx x=let rec fN n g=seq{yield! {n+x..10..g-1}|>Seq.map(fun g->(max 1 n,g)); yield! fN(g)(g*10)} in fN 0 10|>Seq.filter(fG>>not)|>Seq.map snd
let uPx x=let rec fN n g=seq{yield! {n+x..10..g-1}|>Seq.map(fun g->(max 1 n,g)); yield! fN(g)(g*10)} in fN 0 10|>Seq.filter(fG>>not)|>Seq.map snd
[0..9]|>Seq.iter(fun n->printfn "first umprimable number ending in %d is %d" n (uPx n|>Seq.head))
[0..9]|>Seq.iter(fun n->printfn "first umprimable number ending in %d is %d" n (uPx n|>Seq.head))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 773: Line 773:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2019-10-06}}
{{works with|Factor|0.99 2019-10-06}}
<lang factor>USING: assocs formatting io kernel lists lists.lazy
<syntaxhighlight lang="factor">USING: assocs formatting io kernel lists lists.lazy
lists.lazy.examples math math.functions math.primes math.ranges
lists.lazy.examples math math.functions math.primes math.ranges
math.text.utils prettyprint sequences tools.memory.private ;
math.text.utils prettyprint sequences tools.memory.private ;
Line 805: Line 805:


"The first unprimeable number ending with" print
"The first unprimeable number ending with" print
first-digits [ commas " %d: %9s\n" printf ] assoc-each</lang>
first-digits [ commas " %d: %9s\n" printf ] assoc-each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 827: Line 827:
</pre>
</pre>
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
Function isprime(n As Ulongint) As boolean
Function isprime(n As Ulongint) As boolean
If (n=2) Or (n=3) Then Return 1
If (n=2) Or (n=3) Then Return 1
Line 879: Line 879:
Next z
Next z
sleep
sleep
</syntaxhighlight>
</lang>
<pre>
<pre>
First 35
First 35
Line 898: Line 898:
=={{header|Go}}==
=={{header|Go}}==
Simple brute force (no sieves, memoization or bigint.ProbablyPrime) as there is not much need for speed here.
Simple brute force (no sieves, memoization or bigint.ProbablyPrime) as there is not much need for speed here.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 984: Line 984:
fmt.Printf(" %d is: %9s\n", i, commatize(firstNum[i]))
fmt.Printf(" %d is: %9s\n", i, commatize(firstNum[i]))
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,005: Line 1,005:
</pre>
</pre>
=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Control.Lens ((.~), ix, (&))
<syntaxhighlight lang="haskell">import Control.Lens ((.~), ix, (&))
import Data.Numbers.Primes (isPrime)
import Data.Numbers.Primes (isPrime)
import Data.List (find, intercalate)
import Data.List (find, intercalate)
Line 1,035: Line 1,035:
lowest n = do
lowest n = do
x <- find (\x -> x `mod` 10 == n) unPrimeable
x <- find (\x -> x `mod` 10 == n) unPrimeable
pure (n, thousands $ show x)</lang>
pure (n, thousands $ show x)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,057: Line 1,057:
=={{header|J}}==
=={{header|J}}==
Reshaping data is a strength of j.
Reshaping data is a strength of j.
<syntaxhighlight lang="j">
<lang J>
NB. replace concatenates at various ranks and in boxes to avoid fill
NB. replace concatenates at various ranks and in boxes to avoid fill
NB. the curtailed prefixes (}:\) with all of 0..9 (i.10) with the beheaded suffixes (}.\.)
NB. the curtailed prefixes (}:\) with all of 0..9 (i.10) with the beheaded suffixes (}.\.)
Line 1,069: Line 1,069:


assert 0 1 -: unprimable 193 200
assert 0 1 -: unprimable 193 200
</syntaxhighlight>
</lang>
<pre>
<pre>
NB. test 2e6 integers for unprimability
NB. test 2e6 integers for unprimability
Line 1,089: Line 1,089:


=={{header|Java}}==
=={{header|Java}}==
<lang java>
<syntaxhighlight lang="java">
public class UnprimeableNumbers {
public class UnprimeableNumbers {


Line 1,185: Line 1,185:


}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,209: Line 1,209:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
Auxiliary function:
Auxiliary function:
<lang javascript>
<syntaxhighlight lang="javascript">
Number.prototype.isPrime = function() {
Number.prototype.isPrime = function() {
let i = 2, num = this;
let i = 2, num = this;
Line 1,220: Line 1,220:
return true;
return true;
}
}
</syntaxhighlight>
</lang>


Core function:
Core function:
<lang javascript>
<syntaxhighlight lang="javascript">
function isUnprimable(num) {
function isUnprimable(num) {
if (num < 100 || num.isPrime()) return false;
if (num < 100 || num.isPrime()) return false;
Line 1,237: Line 1,237:
return true;
return true;
}
}
</syntaxhighlight>
</lang>


Main function for output:
Main function for output:
<lang javascript>
<syntaxhighlight lang="javascript">
let unprimeables = [],
let unprimeables = [],
endings = new Array(10).fill('-'),
endings = new Array(10).fill('-'),
Line 1,270: Line 1,270:
}
}
console.timeEnd('II');
console.timeEnd('II');
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,301: Line 1,301:


'''Preliminaries'''
'''Preliminaries'''
<lang jq>def digits: tostring | explode | map([.] | implode | tonumber);
<syntaxhighlight lang="jq">def digits: tostring | explode | map([.] | implode | tonumber);


def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
</syntaxhighlight>
</lang>
'''Unprimeables'''
'''Unprimeables'''
<syntaxhighlight lang="jq">
<lang jq>
def variants:
def variants:
digits
digits
Line 1,322: Line 1,322:


def unprimeables:
def unprimeables:
range(4; infinite) | select(is_unprimeable);</lang>
range(4; infinite) | select(is_unprimeable);</syntaxhighlight>


'''The Tasks'''
'''The Tasks'''
<lang jq>def task:
<syntaxhighlight lang="jq">def task:
"First 35 unprimeables: ",
"First 35 unprimeables: ",
[limit(35; range(0;infinite) | select(is_unprimeable))],
[limit(35; range(0;infinite) | select(is_unprimeable))],
Line 1,338: Line 1,338:
;
;


task</lang>
task</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,362: Line 1,362:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes, Lazy, Formatting
<syntaxhighlight lang="julia">using Primes, Lazy, Formatting


function isunprimeable(n)
function isunprimeable(n)
Line 1,386: Line 1,386:
println(" $dig ", lpad(format(n, commas=true), 9))
println(" $dig ", lpad(format(n, commas=true), 9))
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
First 35 unprimeables: (200 204 206 208 320 322 324 325 326 328 510 512 514 515 516 518 530 532 534 535 536 538 620 622 624 625 626 628 840 842 844 845 846 848 890)
First 35 unprimeables: (200 204 206 208 320 322 324 325 326 328 510 512 514 515 516 518 530 532 534 535 536 538 620 622 624 625 626 628 840 842 844 845 846 848 890)
Line 1,408: Line 1,408:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>private const val MAX = 10000000
<syntaxhighlight lang="scala">private const val MAX = 10000000
private val primes = BooleanArray(MAX)
private val primes = BooleanArray(MAX)


Line 1,502: Line 1,502:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 35 unprimeable numbers:
<pre>First 35 unprimeable numbers:
Line 1,522: Line 1,522:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>-- FUNCS:
<syntaxhighlight lang="lua">-- FUNCS:
local function T(t) return setmetatable(t, {__index=table}) end
local function T(t) return setmetatable(t, {__index=table}) end
table.filter = function(t,f) local s=T{} for _,v in ipairs(t) do if f(v) then s[#s+1]=v end end return s end
table.filter = function(t,f) local s=T{} for _,v in ipairs(t) do if f(v) then s[#s+1]=v end end return s end
Line 1,571: Line 1,571:
for i = 0, 9 do
for i = 0, 9 do
print(" " .. i .. " is: " .. commafy(lowests[i]))
print(" " .. i .. " is: " .. commafy(lowests[i]))
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 35 unprimable numbers are:
<pre>The first 35 unprimable numbers are:
Line 1,591: Line 1,591:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>ClearAll[Unprimeable]
<syntaxhighlight lang="mathematica">ClearAll[Unprimeable]
Unprimeable[in_Integer] := Module[{id, new, pos},
Unprimeable[in_Integer] := Module[{id, new, pos},
id = IntegerDigits[in];
id = IntegerDigits[in];
Line 1,633: Line 1,633:


lastdigit = IntegerDigits /* Last;
lastdigit = IntegerDigits /* Last;
Print["Least unprimeable number ending in ", lastdigit[#], ": ", #] & /@ SortBy[out, lastdigit];</lang>
Print["Least unprimeable number ending in ", lastdigit[#], ": ", #] & /@ SortBy[out, lastdigit];</syntaxhighlight>
{{out}}
{{out}}
<pre>{200,204,206,208,320,322,324,325,326,328,510,512,514,515,516,518,530,532,534,535,536,538,620,622,624,625,626,628,840,842,844,845,846,848,890}
<pre>{200,204,206,208,320,322,324,325,326,328,510,512,514,515,516,518,530,532,534,535,536,538,620,622,624,625,626,628,840,842,844,845,846,848,890}
Line 1,649: Line 1,649:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strutils
<syntaxhighlight lang="nim">import strutils


const N = 10_000_000
const N = 10_000_000
Line 1,697: Line 1,697:
while not n.isUmprimeable:
while not n.isUmprimeable:
inc n, 10
inc n, 10
echo "Lowest unprimeable number ending in ", d, " is ", ($n).insertSep(',')</lang>
echo "Lowest unprimeable number ending in ", d, " is ", ($n).insertSep(',')</syntaxhighlight>


{{out}}
{{out}}
Line 1,719: Line 1,719:
{{trans|Go}} {{works with|Free Pascal}}{{works with|Delphi}}
{{trans|Go}} {{works with|Free Pascal}}{{works with|Delphi}}
Small improvement.When the check of value ending in "0" is not unprimable than I can jump over by 10, since the check already has checked those numbers ending in "1".."9".But in case of unprimable I am using a reduced version of the check<BR>Results in runtime reduced from 1.8 secs downto 0.667 now to 0.46
Small improvement.When the check of value ending in "0" is not unprimable than I can jump over by 10, since the check already has checked those numbers ending in "1".."9".But in case of unprimable I am using a reduced version of the check<BR>Results in runtime reduced from 1.8 secs downto 0.667 now to 0.46
<lang pascal>program unprimable;
<syntaxhighlight lang="pascal">program unprimable;
{$IFDEF FPC}{$Mode Delphi}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
{$IFDEF FPC}{$Mode Delphi}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
Line 1,930: Line 1,930:
writeln('There are ',TotalCnt,' unprimable numbers upto ',n);
writeln('There are ',TotalCnt,' unprimable numbers upto ',n);
{$IFNDEF UNIX}readln;{$ENDIF}
{$IFNDEF UNIX}readln;{$ENDIF}
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre style="font-size:84%">
<pre style="font-size:84%">
Line 1,953: Line 1,953:
Base 18= 2*3*3 :lowest digit 7 found first 10,921,015,789<BR>
Base 18= 2*3*3 :lowest digit 7 found first 10,921,015,789<BR>
bases that are prime find their different digits quite early.
bases that are prime find their different digits quite early.
<lang pascal>program unprimable;
<syntaxhighlight lang="pascal">program unprimable;
{$IFDEF FPC}
{$IFDEF FPC}
{$Mode Delphi}
{$Mode Delphi}
Line 2,369: Line 2,369:
writeln('There are ',TotalCnt,' unprimable numbers upto ',n);
writeln('There are ',TotalCnt,' unprimable numbers upto ',n);
setlength(Primes,0);
setlength(Primes,0);
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:35ex">
<pre style="height:35ex">
Line 2,441: Line 2,441:
{{trans|Raku}}
{{trans|Raku}}
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 2,470: Line 2,470:
while ($x += 10) { last if is_unprimeable($x) }
while ($x += 10) { last if is_unprimeable($x) }
say "First unprimeable that ends with $_: " . sprintf "%9s", comma $x;
say "First unprimeable that ends with $_: " . sprintf "%9s", comma $x;
} 0..9;</lang>
} 0..9;</syntaxhighlight>
{{out}}
{{out}}
<pre>First 35 unprimeables:
<pre>First 35 unprimeables:
Line 2,490: Line 2,490:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Go}}
{{trans|Go}}
<!--<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: #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 35 unprimeable numbers are:\n"</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 35 unprimeable numbers are:\n"</span><span style="color: #0000FF;">)</span>
Line 2,543: Line 2,543:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,566: Line 2,566:
=={{header|Pike}}==
=={{header|Pike}}==
Not the fastest way of doing this, but using string manipulation seemed like the obvious Pike way to do it.
Not the fastest way of doing this, but using string manipulation seemed like the obvious Pike way to do it.
<syntaxhighlight lang="pike">
<lang Pike>
bool is_unprimeable(int i)
bool is_unprimeable(int i)
{
{
Line 2,605: Line 2,605:
write(" %s is: %9d\n", e, first_enders[e]);
write(" %s is: %9d\n", e, first_enders[e]);
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 2,626: Line 2,626:


=={{header|Python}}==
=={{header|Python}}==
<lang python>from itertools import count, islice
<syntaxhighlight lang="python">from itertools import count, islice


def primes(_cache=[2, 3]):
def primes(_cache=[2, 3]):
Line 2,677: Line 2,677:


for i,v in enumerate(first):
for i,v in enumerate(first):
print(f'{i} ending: {v}')</lang>
print(f'{i} ending: {v}')</syntaxhighlight>
{{out}}
{{out}}
<pre>First 35:
<pre>First 35:
Line 2,699: Line 2,699:
=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(require math/number-theory)
(require math/number-theory)
Line 2,745: Line 2,745:
(check-equal? (primeable? 10) 11)
(check-equal? (primeable? 10) 11)
(check-true (unprimeable? 200))
(check-true (unprimeable? 200))
(check-false (unprimeable? 201)))</lang>
(check-false (unprimeable? 201)))</syntaxhighlight>


{{out}}
{{out}}
Line 2,765: Line 2,765:
(formerly Perl 6)
(formerly Perl 6)
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl6>use ntheory:from<Perl5> <is_prime>;
<syntaxhighlight lang="raku" line>use ntheory:from<Perl5> <is_prime>;
use Lingua::EN::Numbers;
use Lingua::EN::Numbers;


Line 2,791: Line 2,791:
print "First unprimeable that ends with {n}: " ~
print "First unprimeable that ends with {n}: " ~
sprintf "%9s\n", comma (n, *+10 … *).race.first: { .&is-unprimeable }
sprintf "%9s\n", comma (n, *+10 … *).race.first: { .&is-unprimeable }
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 35 unprimeables:
<pre>First 35 unprimeables:
Line 2,813: Line 2,813:


With the addition of the computation of squared primes, &nbsp; the program now is about '''4''' times faster.
With the addition of the computation of squared primes, &nbsp; the program now is about '''4''' times faster.
<lang rexx>/*REXX program finds and displays unprimeable numbers (non─negative integers). */
<syntaxhighlight lang="rexx">/*REXX program finds and displays unprimeable numbers (non─negative integers). */
parse arg n x hp . /*obtain optional arguments from the CL*/
parse arg n x hp . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 35 /*Not specified? Then use the default.*/
if n=='' | n=="," then n= 35 /*Not specified? Then use the default.*/
Line 2,880: Line 2,880:
end /*k*/ /* [↓] a prime (J) has been found. */
end /*k*/ /* [↓] a prime (J) has been found. */
#= #+1; if #<=lim then @.#=j; !.j=1 /*bump prime count; assign prime to @. */
#= #+1; if #<=lim then @.#=j; !.j=1 /*bump prime count; assign prime to @. */
sq.#= j*j /*calculate square of J for fast WHILE.*/</lang>
sq.#= j*j /*calculate square of J for fast WHILE.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}


Line 2,905: Line 2,905:
=={{header|Rust}}==
=={{header|Rust}}==
{{trans|C++}}
{{trans|C++}}
<lang rust>// main.rs
<syntaxhighlight lang="rust">// main.rs
mod bit_array;
mod bit_array;
mod prime_sieve;
mod prime_sieve;
Line 2,981: Line 2,981:
println!("Least unprimeable number ending in {}: {}", i, lowest[i]);
println!("Least unprimeable number ending in {}: {}", i, lowest[i]);
}
}
}</lang>
}</syntaxhighlight>


<lang rust>// prime_sieve.rs
<syntaxhighlight lang="rust">// prime_sieve.rs
use crate::bit_array;
use crate::bit_array;


Line 3,018: Line 3,018:
!self.composite.get(n / 2 - 1)
!self.composite.get(n / 2 - 1)
}
}
}</lang>
}</syntaxhighlight>


<lang rust>// bit_array.rs
<syntaxhighlight lang="rust">// bit_array.rs
pub struct BitArray {
pub struct BitArray {
array: Vec<u32>,
array: Vec<u32>,
Line 3,043: Line 3,043:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,063: Line 3,063:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func is_unprimeable(n) {
<syntaxhighlight lang="ruby">func is_unprimeable(n) {
var t = 10*floor(n/10)
var t = 10*floor(n/10)
for k in (t+1 .. t+9 `by` 2) {
for k in (t+1 .. t+9 `by` 2) {
Line 3,094: Line 3,094:
say ("First unprimeable that ends with #{d}: ",
say ("First unprimeable that ends with #{d}: ",
1..Inf -> lazy.map {|k| k*10 + d }.grep(is_unprimeable).first)
1..Inf -> lazy.map {|k| k*10 + d }.grep(is_unprimeable).first)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,116: Line 3,116:
=={{header|Swift}}==
=={{header|Swift}}==
{{trans|Rust}}
{{trans|Rust}}
<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


class BitArray {
class BitArray {
Line 3,242: Line 3,242:
let str = NumberFormatter.localizedString(from: number, number: .decimal)
let str = NumberFormatter.localizedString(from: number, number: .decimal)
print("Least unprimeable number ending in \(i): \(str)")
print("Least unprimeable number ending in \(i): \(str)")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,265: Line 3,265:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
{{libheader|Wren-math}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/math" for Int
import "/math" for Int


Line 3,310: Line 3,310:
System.print("The first unprimeable number that ends in:")
System.print("The first unprimeable number that ends in:")
for (i in 0...10) System.print(" %(i) is: %(Fmt.dc(9, firstNum[i]))")</lang>
for (i in 0...10) System.print(" %(i) is: %(Fmt.dc(9, firstNum[i]))")</syntaxhighlight>


{{out}}
{{out}}
Line 3,333: Line 3,333:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is prime
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
int N, I;
[if N <= 2 then return N = 2;
[if N <= 2 then return N = 2;
Line 3,391: Line 3,391:
];
];
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 3,413: Line 3,413:
{{trans|Sidef}}
{{trans|Sidef}}
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library and fast prime checking
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library and fast prime checking
<lang zkl>var [const] BI=Import("zklBigNum"); // libGMP
<syntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP


fcn isUnprimeable(n){ //--> n (!0) or Void, a filter
fcn isUnprimeable(n){ //--> n (!0) or Void, a filter
Line 3,428: Line 3,428:
n
n
}
}
fcn isUnprimeableW{ [100..].tweak(isUnprimeable) } // --> iterator</lang>
fcn isUnprimeableW{ [100..].tweak(isUnprimeable) } // --> iterator</syntaxhighlight>
<lang zkl>isUnprimeableW().walk(35).concat(" ").println();
<syntaxhighlight lang="zkl">isUnprimeableW().walk(35).concat(" ").println();
println("The 600th unprimeable number is: %,d".fmt(isUnprimeableW().drop(600).value));
println("The 600th unprimeable number is: %,d".fmt(isUnprimeableW().drop(600).value));


Line 3,436: Line 3,436:
{ d:=up%10; if(ups[d]==0){ ups[d]=up; if((s-=1)<=0) break; } }
{ d:=up%10; if(ups[d]==0){ ups[d]=up; if((s-=1)<=0) break; } }
println("The first unprimeable number that ends in:");
println("The first unprimeable number that ends in:");
foreach n in (10){ println("%d is %8,d".fmt(n,ups[n])) }</lang>
foreach n in (10){ println("%d is %8,d".fmt(n,ups[n])) }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>