Unprimeable numbers: Difference between revisions

m
m (→‎{{header|Phix}}: added syntax colouring)
m (→‎{{header|Wren}}: Minor tidy)
 
(11 intermediate revisions by 8 users not shown)
Line 55:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V limit = 10'000'000
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 102:
 
L(v) first
print(L.index‘ ending: ’v)</langsyntaxhighlight>
 
{{out}}
Line 123:
9 ending: 212159
</pre>
 
=={{header|ALGOL 68}}==
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}}
<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 #
PR read "primes.incl.a68" PR
INT max prime = 9 999 999;
[]BOOL prime = PRIMESIEVE max prime;
# returns TRUE if n is unprimeable, FALSE otherwise #
PROC is unprimeable = ( INT n )BOOL:
IF n < 100
THEN FALSE
ELIF prime[ n ]
THEN FALSE
ELIF
# need to try changing a digit #
INT last digit = n MOD 10;
INT leading digits = n - last digit;
prime[ leading digits + 1 ]
THEN FALSE
ELIF prime[ leading digits + 3 ] THEN FALSE
ELIF prime[ leading digits + 7 ] THEN FALSE
ELIF prime[ leading digits + 9 ] THEN FALSE
ELIF last digit = 2 OR last digit = 5
THEN
# the final digit is 2 or 5, changing the other digits can't make a prime #
# unless there is only one other digit which we change to 0 #
INT v := leading digits;
INT dc := 1;
WHILE ( v OVERAB 10 ) > 0 DO IF v MOD 10 /= 0 THEN dc +:= 1 FI OD;
dc /= 2
ELIF NOT ODD last digit
THEN TRUE # last digit is even - can't make a prime #
ELSE
# last digit is 1, 3, 7, 9: must try changing the other digoits #
INT m10 := 10;
INT r10 := 100;
BOOL result := TRUE;
WHILE result AND n > r10 DO
INT base = ( ( n OVER r10 ) * r10 ) + ( n MOD m10 );
FOR i FROM 0 BY m10 WHILE result AND i < r10 DO
result := NOT prime[ base + i ]
OD;
m10 *:= 10;
r10 *:= 10
OD;
IF result THEN
# still not unprimeable, try changing the first digit #
INT base = n MOD m10;
FOR i FROM 0 BY m10 WHILE result AND i < r10 DO
result := NOT prime[ base + i ]
OD
FI;
result
FI # is unprimeable # ;
# returns a string representation of n with commas #
PROC commatise = ( LONG LONG INT n )STRING:
BEGIN
STRING result := "";
STRING unformatted = whole( n, 0 );
INT ch count := 0;
FOR c FROM UPB unformatted BY -1 TO LWB unformatted DO
IF ch count <= 2 THEN ch count +:= 1
ELSE ch count := 1; "," +=: result
FI;
unformatted[ c ] +=: result
OD;
result
END; # commatise #
# find unprimeable numbers #
INT u count := 0;
INT d count := 0;
[ 0 : 9 ]INT first unprimeable := []INT( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )[ AT 0 ];
FOR i FROM 100 WHILE i < UPB prime AND d count < 10 DO
IF is unprimeable( i ) THEN
u count +:= 1;
IF u count = 1 THEN
print( ( "First 35 unprimeable numbers: ", whole( i, 0 ) ) )
ELIF u count <= 35 THEN
print( ( " ", whole( i, 0 ) ) )
ELIF u count = 600 THEN
print( ( newline, "600th unprimeable number: ", commatise( i ) ) )
FI;
INT final digit = i MOD 10;
IF first unprimeable[ final digit ] = 0 THEN
# first unprimeable number with this final digit #
d count +:= 1;
first unprimeable[ final digit ] := i
FI
FI
OD;
# show the first unprimeable number that ends with each digit #
print( ( newline ) );
FOR i FROM 0 TO 9 DO
print( ( "First unprimeable number ending in "
, whole( i, 0 )
, ": "
, commatise( first unprimeable[ i ] )
, newline
)
)
OD
END</syntaxhighlight>
{{out}}
<pre>
First 35 unprimeable numbers: 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
600th unprimeable number: 5,242
First unprimeable number ending in 0: 200
First unprimeable number ending in 1: 595,631
First unprimeable number ending in 2: 322
First unprimeable number ending in 3: 1,203,623
First unprimeable number ending in 4: 204
First unprimeable number ending in 5: 325
First unprimeable number ending in 6: 206
First unprimeable number ending in 7: 872,897
First unprimeable number ending in 8: 208
First unprimeable number ending in 9: 212,159
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">unprimeable?: function [n][
if prime? n -> return false
nd: to :string n
loop.with:'i nd 'prevDigit [
loop `0`..`9` 'newDigit [
if newDigit <> prevDigit [
nd\[i]: newDigit
if prime? to :integer nd -> return false
]
]
nd\[i]: prevDigit
]
return true
]
 
cnt: 0
x: 1
unprimeables: []
while [cnt < 600][
if unprimeable? x [
unprimeables: unprimeables ++ x
cnt: cnt + 1
]
x: x + 1
]
 
print "First 35 unprimeable numbers:"
print first.n: 35 unprimeables
print ""
print ["600th unprimeable number:" last unprimeables]</syntaxhighlight>
 
{{out}}
 
<pre>First 35 unprimeable numbers:
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
 
600th unprimeable number: 5242</pre>
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <assert.h>
#include <locale.h>
#include <stdbool.h>
Line 256 ⟶ 415:
printf("\n600th unprimeable number: %'u\n", n);
uint32_t last_digit = n % 10;
if (lowest[last_digit] == 0) {
{
lowest[last_digit] = n;
++found;
Line 267 ⟶ 425:
printf("Least unprimeable number ending in %u: %'u\n" , i, lowest[i]);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 287 ⟶ 445:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
#include "prime_sieve.hpp"
Line 356 ⟶ 514:
std::cout << "Least unprimeable number ending in " << i << ": " << lowest[i] << '\n';
return 0;
}</langsyntaxhighlight>
 
Contents of prime_sieve.hpp:
<langsyntaxhighlight lang="cpp">#ifndef PRIME_SIEVE_HPP
#define PRIME_SIEVE_HPP
 
Line 410 ⟶ 568:
}
 
#endif</langsyntaxhighlight>
 
{{out}}
Line 431 ⟶ 589:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.array;
import std.conv;
Line 530 ⟶ 688:
writefln(" %d is %,d", i, v);
}
}</langsyntaxhighlight>
{{out}}
<pre>First 35 unprimeable numbers:
Line 555 ⟶ 713:
===The function===
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 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 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
</syntaxhighlight>
</lang>
===The Task===
<langsyntaxhighlight lang="fsharp">
uP()|>Seq.take 35|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{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>
<langsyntaxhighlight lang="fsharp">
printfn "600th unprimable number is %d" (uP()|>Seq.item 599)
</syntaxhighlight>
</lang>
{{out}}
<pre>
600th unprimable number is 5242
</pre>
<langsyntaxhighlight 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)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 595 ⟶ 753:
===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.
<langsyntaxhighlight 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
[0..9]|>Seq.iter(fun n->printfn "first umprimable number ending in %d is %d" n (uPx n|>Seq.head))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 615 ⟶ 773:
=={{header|Factor}}==
{{works with|Factor|0.99 2019-10-06}}
<langsyntaxhighlight lang="factor">USING: assocs formatting io kernel lists lists.lazy
lists.lazy.examples math math.functions math.primes math.ranges
math.text.utils prettyprint sequences tools.memory.private ;
Line 647 ⟶ 805:
 
"The first unprimeable number ending with" print
first-digits [ commas " %d: %9s\n" printf ] assoc-each</langsyntaxhighlight>
{{out}}
<pre>
Line 669 ⟶ 827:
</pre>
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Function isprime(n As Ulongint) As boolean
If (n=2) Or (n=3) Then Return 1
Line 721 ⟶ 879:
Next z
sleep
</syntaxhighlight>
</lang>
<pre>
First 35
Line 740 ⟶ 898:
=={{header|Go}}==
Simple brute force (no sieves, memoization or bigint.ProbablyPrime) as there is not much need for speed here.
<langsyntaxhighlight lang="go">package main
 
import (
Line 826 ⟶ 984:
fmt.Printf(" %d is: %9s\n", i, commatize(firstNum[i]))
}
}</langsyntaxhighlight>
 
{{out}}
Line 847 ⟶ 1,005:
</pre>
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Lens ((.~), ix, (&))
import Data.Numbers.Primes (isPrime)
import Data.List (find, intercalate)
Line 877 ⟶ 1,035:
lowest n = do
x <- find (\x -> x `mod` 10 == n) unPrimeable
pure (n, thousands $ show x)</langsyntaxhighlight>
{{out}}
<pre>
Line 899 ⟶ 1,057:
=={{header|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. the curtailed prefixes (}:\) with all of 0..9 (i.10) with the beheaded suffixes (}.\.)
Line 911 ⟶ 1,069:
 
assert 0 1 -: unprimable 193 200
</syntaxhighlight>
</lang>
<pre>
NB. test 2e6 integers for unprimability
Line 931 ⟶ 1,089:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
public class UnprimeableNumbers {
 
Line 1,027 ⟶ 1,185:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,051 ⟶ 1,209:
=={{header|JavaScript}}==
Auxiliary function:
<langsyntaxhighlight lang="javascript">
Number.prototype.isPrime = function() {
let i = 2, num = this;
Line 1,062 ⟶ 1,220:
return true;
}
</syntaxhighlight>
</lang>
 
Core function:
<langsyntaxhighlight lang="javascript">
function isUnprimable(num) {
if (num < 100 || num.isPrime()) return false;
Line 1,079 ⟶ 1,237:
return true;
}
</syntaxhighlight>
</lang>
 
Main function for output:
<langsyntaxhighlight lang="javascript">
let unprimeables = [],
endings = new Array(10).fill('-'),
Line 1,112 ⟶ 1,270:
}
console.timeEnd('II');
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,135 ⟶ 1,293:
II: 186884ms - timer ended
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable definition of `is_prime` as used here.
 
'''Preliminaries'''
<syntaxhighlight lang="jq">def digits: tostring | explode | map([.] | implode | tonumber);
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
</syntaxhighlight>
'''Unprimeables'''
<syntaxhighlight lang="jq">
def variants:
digits
| range(0; length) as $pos
| range(0;10) as $newdigit
| if .[$pos] == $newdigit then empty
else .[$pos] = $newdigit
| join("")|tonumber
end;
def is_unprimeable:
if is_prime or any(variants; is_prime) then false
else true
end;
 
def unprimeables:
range(4; infinite) | select(is_unprimeable);</syntaxhighlight>
 
'''The Tasks'''
<syntaxhighlight lang="jq">def task:
"First 35 unprimeables: ",
[limit(35; range(0;infinite) | select(is_unprimeable))],
 
"\nThe 600th unprimeable is \( nth(600 - 1; unprimeables) ).",
 
"\nDigit First unprimeable ending with that digit",
"-----------------------------------------------",
(range(0;10) as $dig
| first( range(0;infinite) | select((. % 10 == $dig) and is_unprimeable))
| " \($dig) \(lpad(9))" )
;
 
task</syntaxhighlight>
{{out}}
<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]
 
The 600th unprimeable is 5242.
 
Digit First unprimeable ending with that digit
-----------------------------------------------
0 200
1 595631
2 322
3 1203623
4 204
5 325
6 206
7 872897
8 208
9 212159
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes, Lazy, Formatting
 
function isunprimeable(n)
Line 1,161 ⟶ 1,386:
println(" $dig ", lpad(format(n, commas=true), 9))
end
</langsyntaxhighlight>{{out}}
<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)
Line 1,183 ⟶ 1,408:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">private const val MAX = 10000000
private val primes = BooleanArray(MAX)
 
Line 1,277 ⟶ 1,502:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 35 unprimeable numbers:
Line 1,297 ⟶ 1,522:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- FUNCS:
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
Line 1,346 ⟶ 1,571:
for i = 0, 9 do
print(" " .. i .. " is: " .. commafy(lowests[i]))
end</langsyntaxhighlight>
{{out}}
<pre>The first 35 unprimable numbers are:
Line 1,366 ⟶ 1,591:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Unprimeable]
Unprimeable[in_Integer] := Module[{id, new, pos},
id = IntegerDigits[in];
Line 1,408 ⟶ 1,633:
 
lastdigit = IntegerDigits /* Last;
Print["Least unprimeable number ending in ", lastdigit[#], ": ", #] & /@ SortBy[out, lastdigit];</langsyntaxhighlight>
{{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}
Line 1,424 ⟶ 1,649:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils
 
const N = 10_000_000
Line 1,472 ⟶ 1,697:
while not n.isUmprimeable:
inc n, 10
echo "Lowest unprimeable number ending in ", d, " is ", ($n).insertSep(',')</langsyntaxhighlight>
 
{{out}}
Line 1,494 ⟶ 1,719:
{{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
<langsyntaxhighlight lang="pascal">program unprimable;
{$IFDEF FPC}{$Mode Delphi}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
Line 1,705 ⟶ 1,930:
writeln('There are ',TotalCnt,' unprimable numbers upto ',n);
{$IFNDEF UNIX}readln;{$ENDIF}
end.</langsyntaxhighlight>
{{out}}
<pre style="font-size:84%">
Line 1,728 ⟶ 1,953:
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.
<langsyntaxhighlight lang="pascal">program unprimable;
{$IFDEF FPC}
{$Mode Delphi}
Line 2,144 ⟶ 2,369:
writeln('There are ',TotalCnt,' unprimable numbers upto ',n);
setlength(Primes,0);
end.</langsyntaxhighlight>
{{out}}
<pre style="height:35ex">
Line 2,216 ⟶ 2,441:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 2,245 ⟶ 2,470:
while ($x += 10) { last if is_unprimeable($x) }
say "First unprimeable that ends with $_: " . sprintf "%9s", comma $x;
} 0..9;</langsyntaxhighlight>
{{out}}
<pre>First 35 unprimeables:
Line 2,265 ⟶ 2,490:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 2,318 ⟶ 2,543:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,341 ⟶ 2,566:
=={{header|Pike}}==
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)
{
Line 2,380 ⟶ 2,605:
write(" %s is: %9d\n", e, first_enders[e]);
}
}</langsyntaxhighlight>
Output:
<pre>
Line 2,401 ⟶ 2,626:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import count, islice
 
def primes(_cache=[2, 3]):
Line 2,452 ⟶ 2,677:
 
for i,v in enumerate(first):
print(f'{i} ending: {v}')</langsyntaxhighlight>
{{out}}
<pre>First 35:
Line 2,474 ⟶ 2,699:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
Line 2,520 ⟶ 2,745:
(check-equal? (primeable? 10) 11)
(check-true (unprimeable? 200))
(check-false (unprimeable? 201)))</langsyntaxhighlight>
 
{{out}}
Line 2,539 ⟶ 2,764:
=={{header|Raku}}==
(formerly Perl 6)
{{libheader|ntheory}}
{{works with|Rakudo|2019.11}}
<syntaxhighlight lang="raku" line>use ntheory:from<Perl5> <is_prime>;
 
<lang perl6>use ntheory:from<Perl5> <is_prime>;
use Lingua::EN::Numbers;
 
Line 2,549 ⟶ 2,773:
for ^chrs -> \place {
my \pow = 10**(chrs - place - 1);
my \this = n.substr(place, 1) *× pow;
^10 .map: -> \dgt {
next if this == dgt;
return False if is_prime(n - this + dgt *× pow)
}
}
Line 2,567 ⟶ 2,791:
print "First unprimeable that ends with {n}: " ~
sprintf "%9s\n", comma (n, *+10 … *).race.first: { .&is-unprimeable }
}</langsyntaxhighlight>
{{out}}
<pre>First 35 unprimeables:
Line 2,589 ⟶ 2,813:
 
With the addition of the computation of squared primes, &nbsp; the program now is about '''4''' times faster.
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays unprimeable numbers (non─negative integers). */
parse arg n x hp . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 35 /*Not specified? Then use the default.*/
Line 2,656 ⟶ 2,880:
end /*k*/ /* [↓] a prime (J) has been found. */
#= #+1; if #<=lim then @.#=j; !.j=1 /*bump prime count; assign prime to @. */
sq.#= j*j /*calculate square of J for fast WHILE.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
 
Line 2,679 ⟶ 2,903:
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
def unprimable?(n)
digits = %w(0 1 2 3 4 5 6 7 8 9)
s = n.to_s
size = s.size
(size-1).downto(0) do |i|
digits.each do |d|
cand = s.dup
cand[i]=d
return false if cand.to_i.prime?
end
end
true
end
ups = Enumerator.new {|y| (1..).each{|n| y << n if unprimable?(n)} }
 
ar = ups.first(600)
puts "First 35 unprimables:", ar[0,35].join(" ")
puts "\n600th unprimable:", ar.last, ""
(0..9).each do |d|
print "First unprimeable with last digit #{d}: "
puts (1..).detect{|k| unprimable?(k*10+d)}*10 + d
end
</syntaxhighlight>
{{out}}
<pre>First 35 unprimables:
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
 
600th unprimable:
5242
 
First unprimeable with last digit 0: 200
First unprimeable with last digit 1: 595631
First unprimeable with last digit 2: 322
First unprimeable with last digit 3: 1203623
First unprimeable with last digit 4: 204
First unprimeable with last digit 5: 325
First unprimeable with last digit 6: 206
First unprimeable with last digit 7: 872897
First unprimeable with last digit 8: 208
First unprimeable with last digit 9: 212159
</pre>
=={{header|Rust}}==
{{trans|C++}}
<langsyntaxhighlight lang="rust">// main.rs
mod bit_array;
mod prime_sieve;
Line 2,757 ⟶ 3,025:
println!("Least unprimeable number ending in {}: {}", i, lowest[i]);
}
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="rust">// prime_sieve.rs
use crate::bit_array;
 
Line 2,794 ⟶ 3,062:
!self.composite.get(n / 2 - 1)
}
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="rust">// bit_array.rs
pub struct BitArray {
array: Vec<u32>,
Line 2,819 ⟶ 3,087:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,839 ⟶ 3,107:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_unprimeable(n) {
var t = 10*floor(n/10)
for k in (t+1 .. t+9 `by` 2) {
Line 2,870 ⟶ 3,138:
say ("First unprimeable that ends with #{d}: ",
1..Inf -> lazy.map {|k| k*10 + d }.grep(is_unprimeable).first)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,892 ⟶ 3,160:
=={{header|Swift}}==
{{trans|Rust}}
<langsyntaxhighlight lang="swift">import Foundation
 
class BitArray {
Line 3,018 ⟶ 3,286:
let str = NumberFormatter.localizedString(from: number, number: .decimal)
print("Least unprimeable number ending in \(i): \(str)")
}</langsyntaxhighlight>
 
{{out}}
Line 3,041 ⟶ 3,309:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./math" for Int
 
System.print("The first 35 unprimeable numbers are:")
Line 3,086 ⟶ 3,354:
System.print("The first unprimeable number that ends in:")
for (i in 0...10) System.print(" %(i) is: %(Fmt.dc(9, firstNum[i]))")</langsyntaxhighlight>
 
{{out}}
Line 3,106 ⟶ 3,374:
8 is: 208
9 is: 212,159
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
 
func Unprimeable(N); \Return 'true' if N is unprimeable
int N, I, J, Len, D, SD;
char Num(10);
[I:= 0; \take N apart
repeat N:= N/10;
Num(I):= rem(0);
I:= I+1;
until N = 0;
Len:= I; \number of digits in N (length)
for J:= 0 to Len-1 do
[SD:= Num(J); \save digit
for D:= 0 to 9 do \replace with all digits
[Num(J):= D;
N:= 0; \rebuild N
for I:= Len-1 downto 0 do
N:= N*10 + Num(I);
if IsPrime(N) then return false;
];
Num(J):= SD; \restore saved digit
];
return true;
];
 
int C, N, D;
[Text(0, "First 35 unprimeables:^m^j");
C:= 0;
N:= 100;
loop [if Unprimeable(N) then
[C:= C+1;
if C <= 35 then
[IntOut(0, N); ChOut(0, ^ )];
if C = 600 then quit;
];
N:= N+1;
];
Text(0, "^m^j600th unprimeable: ");
IntOut(0, N); CrLf(0);
for D:= 0 to 9 do
[IntOut(0, D); Text(0, ": ");
N:= 100 + D;
loop [if Unprimeable(N) then
[IntOut(0, N); CrLf(0);
quit;
];
N:= N+10;
];
];
]</syntaxhighlight>
 
{{out}}
<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
600th unprimeable: 5242
0: 200
1: 595631
2: 322
3: 1203623
4: 204
5: 325
6: 206
7: 872897
8: 208
9: 212159
</pre>
 
Line 3,111 ⟶ 3,457:
{{trans|Sidef}}
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library and fast prime checking
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
 
fcn isUnprimeable(n){ //--> n (!0) or Void, a filter
Line 3,126 ⟶ 3,472:
n
}
fcn isUnprimeableW{ [100..].tweak(isUnprimeable) } // --> iterator</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">isUnprimeableW().walk(35).concat(" ").println();
println("The 600th unprimeable number is: %,d".fmt(isUnprimeableW().drop(600).value));
 
Line 3,134 ⟶ 3,480:
{ d:=up%10; if(ups[d]==0){ ups[d]=up; if((s-=1)<=0) break; } }
println("The first unprimeable number that ends in:");
foreach n in (10){ println("%d is %8,d".fmt(n,ups[n])) }</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits