Unprimeable numbers: Difference between revisions

m
(→‎{{header|REXX}}: simplified parts of the program, optimized for speed.)
m (→‎{{header|Wren}}: Minor tidy)
 
(45 intermediate revisions by 23 users not shown)
Line 51:
:*   from the Adam Spencer book   (page 200):   ''Adam Spencer's World of Numbers''       (Xoum Publishing)
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V limit = 10'000'000
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
I is_prime[n]
L(i) (n * n .< limit + 1).step(n)
is_prime[i] = 0B
 
F unprimeable(a)
I :is_prime[a]
R 0B
V d = 1
L d <= a
V base = (a I/ (d * 10)) * (d * 10) + (a % d)
I any((base .< base + d * 10).step(d).map(y -> :is_prime[y]))
R 0B
d *= 10
R 1B
 
F unprime(n)
[Int] r
L(a) 1..
I unprimeable(a)
r [+]= a
I r.len == n
L.break
R r
 
print(‘First 35:’)
print(unprime(35).map(i -> String(i)).join(‘ ’))
 
print("\nThe 600-th:")
print(unprime(600).last)
print()
 
V first = [0] * 10
V need = 10
L(p) 1..
I unprimeable(p)
V i = p % 10
I first[i] != 0
L.continue
 
first[i] = p
I --need == 0
L.break
 
L(v) first
print(L.index‘ ending: ’v)</syntaxhighlight>
 
{{out}}
<pre>
First 35:
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 600-th:
5242
 
0 ending: 200
1 ending: 595631
2 ending: 322
3 ending: 1203623
4 ending: 204
5 ending: 325
6 ending: 206
7 ending: 872897
8 ending: 208
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 103 ⟶ 334:
 
bool sieve_create(sieve* s, uint32_t limit) {
limit = 2 * (limit/2) + 1;
if (!bit_array_create(&s->not_prime, limit/2))
return false;
Line 185 ⟶ 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 196 ⟶ 425:
printf("Least unprimeable number ending in %u: %'u\n" , i, lowest[i]);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 216 ⟶ 445:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdint>
#include "sieve_of_eratosthenesprime_sieve.hhpp"
 
typedef uint32_t integer;
 
// return number of decimal digits
int count_digits(integer n) {
{
int digits = 0;
for (; n > 0; ++digits)
Line 232 ⟶ 460:
 
// return the number with one digit replaced
integer change_digit(integer n, int index, int new_digit) {
{
integer p = 1;
integer changed = 0;
Line 243 ⟶ 470:
 
// returns true if n unprimeable
bool unprimeable(const sieve_of_eratosthenesprime_sieve& sieve, integer n) {
{
if (sieve.is_prime(n))
return false;
int d = count_digits(n);
for (int i = 0; i < d; ++i) {
for (int j = 0; j <= 9; ++j) {
{
for (int j = 0; j <= 9; ++j)
{
integer m = change_digit(n, i, j);
if (m != n && sieve.is_prime(m))
Line 260 ⟶ 484:
}
 
int main() {
{
const integer limit = 10000000;
sieve_of_eratosthenesprime_sieve sieve(limit);
 
// print numbers with commas
std::cout.imbue(std::locale(""));
std::cout << std::fixed;
 
std::cout << "First 35 unprimeable numbers:\n";
integer n = 100;
integer lowest[10] = { 0 };
for (int count = 0, found = 0; n < limit && (found < 10 || count < 600); ++n) {
if (unprimeable(sieve, n)) {
{
if (unprimeable(sieve,count n)< 35) {
{
if (count < 35)
{
if (count != 0)
std::cout << ", ";
Line 286 ⟶ 505:
std::cout << "\n600th unprimeable number: " << n << '\n';
int last_digit = n % 10;
if (lowest[last_digit] == 0) {
{
lowest[last_digit] = n;
++found;
Line 296 ⟶ 514:
std::cout << "Least unprimeable number ending in " << i << ": " << lowest[i] << '\n';
return 0;
}</langsyntaxhighlight>
 
Contents of sieve_of_eratosthenesprime_sieve.hhpp:
<langsyntaxhighlight lang="cpp">#ifndef SIEVE_OF_ERATOSTHENES_HPRIME_SIEVE_HPP
#define SIEVE_OF_ERATOSTHENES_HPRIME_SIEVE_HPP
 
#include <algorithm>
#include <vector>
 
/**
class sieve_of_eratosthenes {
* A simple implementation of the Sieve of Eratosthenes.
* See https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes.
*/
class prime_sieve {
public:
explicit sieve_of_eratosthenesprime_sieve(size_t);
bool is_prime(size_t) const;
private:
std::vector<bool> odd_prime_is_prime_;
};
 
/**
inline bool sieve_of_eratosthenes::is_prime(size_t n) const {
* Constructs a sieve with the given limit.
if (n == 2)
*
return true;
* @param limit the maximum integer that can be tested for primality
if (n < 2 || n % 2 == 0)
*/
return false;
inline prime_sieve::prime_sieve(size_t limit) {
return odd_prime_[n/2 - 1];
limit = std::max(size_t(3), limit);
}
is_prime_.resize(limit/2, true);
 
inline sieve_of_eratosthenes::sieve_of_eratosthenes(size_t limit) {
limit = std::max(size_t(3), 1 + 2*(limit/2));
odd_prime_.resize(limit/2, true);
for (size_t p = 3; p * p <= limit; p += 2) {
if (odd_prime_is_prime_[p/2 - 1]) {
size_t inc = 2 * p;
for (size_t q = p * p; q <= limit; q += inc)
odd_prime_is_prime_[q/2 - 1] = false;
}
}
}
 
/**
#endif</lang>
* Returns true if the given integer is a prime number. The integer
* must be less than or equal to the limit passed to the constructor.
*
* @param n an integer less than or equal to the limit passed to the
* constructor
* @return true if the integer is prime
*/
inline bool prime_sieve::is_prime(size_t n) const {
if (n == 2)
return true;
if (n < 2 || n % 2 == 0)
return false;
return is_prime_.at(n/2 - 1);
}
 
#endif</syntaxhighlight>
 
{{out}}
Line 352 ⟶ 587:
</pre>
 
=={{header|D}}==
{{trans|Java}}
<syntaxhighlight lang="d">import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
 
immutable MAX = 10_000_000;
bool[] primes;
 
bool[] sieve(int limit) {
bool[] p = uninitializedArray!(bool[])(limit);
p[0..2] = false;
p[2..$] = true;
foreach (i; 2..limit) {
if (p[i]) {
for (int j = 2 * i; j < limit; j += i) {
p[j] = false;
}
}
}
return p;
}
 
string replace(CHAR)(CHAR[] str, int position, CHAR value) {
str[position] = value;
return str.idup;
}
 
bool unPrimeable(int n) {
if (primes[n]) {
return false;
}
auto test = n.to!string;
foreach (i; 0 .. test.length) {
for (char j = '0'; j <= '9'; j++) {
auto r = replace(test.dup, i, j);
if (primes[r.to!int]) {
return false;
}
}
}
return true;
}
 
void displayUnprimeableNumbers(int maxCount) {
int test = 1;
for (int count = 0; count < maxCount;) {
test++;
if (unPrimeable(test)) {
count++;
write(test, ' ');
}
}
writeln;
}
 
int nthUnprimeableNumber(int maxCount) {
int test = 1;
for (int count = 0; count < maxCount;) {
test++;
if (unPrimeable(test)) {
count++;
}
}
return test;
}
 
int[] genLowest() {
int[] lowest = uninitializedArray!(int[])(10);
lowest[] = 0;
 
int count = 0;
int test = 1;
while (count < 10) {
test++;
if (unPrimeable(test) && lowest[test % 10] == 0) {
lowest[test % 10] = test;
count++;
}
}
 
return lowest;
}
 
void main() {
primes = sieve(MAX);
 
writeln("First 35 unprimeable numbers:");
displayUnprimeableNumbers(35);
writeln;
 
int n = 600;
writefln("The %dth unprimeable number = %,d", n, nthUnprimeableNumber(n));
writeln;
 
writeln("Least unprimeable number that ends in:");
foreach (i,v; genLowest()) {
writefln(" %d is %,d", i, v);
}
}</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
 
The 600th unprimeable number = 5,242
 
Least unprimeable number that ends in:
0 is 200
1 is 595,631
2 is 322
3 is 1,203,623
4 is 204
5 is 325
6 is 206
7 is 872,897
8 is 208
9 is 212,159</pre>
 
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Unprimeable_numbers#Pascal Pascal].
 
=={{header|F_Sharp|F#}}==
===The function===
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<syntaxhighlight 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>
===The Task===
<syntaxhighlight lang="fsharp">
uP()|>Seq.take 35|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{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>
<syntaxhighlight lang="fsharp">
printfn "600th unprimable number is %d" (uP()|>Seq.item 599)
</syntaxhighlight>
{{out}}
<pre>
600th unprimable number is 5242
</pre>
<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)))
</syntaxhighlight>
{{out}}
<pre>
first umprimable number ending in 0 is 200
first umprimable number ending in 1 is 595631
first umprimable number ending in 2 is 322
first umprimable number ending in 3 is 1203623
first umprimable number ending in 4 is 204
first umprimable number ending in 5 is 325
first umprimable number ending in 6 is 206
first umprimable number ending in 7 is 872897
first umprimable number ending in 8 is 208
first umprimable number ending in 9 is 212159
Real: 00:01:01.419
</pre>
===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.
<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
[0..9]|>Seq.iter(fun n->printfn "first umprimable number ending in %d is %d" n (uPx n|>Seq.head))
</syntaxhighlight>
{{out}}
<pre>
first umprimable number ending in 0 is 200
first umprimable number ending in 1 is 595631
first umprimable number ending in 2 is 322
first umprimable number ending in 3 is 1203623
first umprimable number ending in 4 is 204
first umprimable number ending in 5 is 325
first umprimable number ending in 6 is 206
first umprimable number ending in 7 is 872897
first umprimable number ending in 8 is 208
first umprimable number ending in 9 is 212159
Real: 00:00:03.158
</pre>
=={{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 380 ⟶ 799:
 
"The first 35 unprimeable numbers:" print bl bl
35 unprimeables ltake list>array [ pprint bl ] eachleach nl nl
 
"The 600th unprimeable number:" print bl bl
unprimeables 599 [ cdr ] timesunprimeables carlnth commas print nl
 
"The first unprimeable number ending with" print
first-digits [ commas " %d: %9s\n" printf ] assoc-each</langsyntaxhighlight>
{{out}}
<pre>
Line 406 ⟶ 825:
8: 208
9: 212,159
</pre>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
Function isprime(n As Ulongint) As boolean
If (n=2) Or (n=3) Then Return 1
If n Mod 2 = 0 Then Return 0
If n Mod 3 = 0 Then Return 0
Dim As Ulongint limit=Sqr(N)+1
For I As Ulongint = 6 To limit Step 6
If N Mod (i-1) = 0 Then Return 0
If N Mod (i+1) = 0 Then Return 0
Next I
Return 1
End Function
 
Sub getnonprimeables(a() As Long)
Dim As String s,g
Dim As Long count,lim=1300000
Redim a(1 To lim)
For num As Long=1 To lim
g=Str(num)
s=g
For n As Long=0 To Len(s)-1
For m As Long=48 To 57
s[n]=m
If isprime(Vallng(s)) Then Goto lbl
Next m
s=g
Next n
count+=1
a(count)=num
lbl:
Next num
Redim Preserve a(1 To count)
End Sub
 
Function endings(n As String,a() As Long) As Long
For m As Long=1 To Ubound(a)
If Right(Str(a(m)),1)=n Then Return a(m)
Next m
End Function
 
Redim As Long n()
getnonprimeables(n())
print " First 35"
For m As Long=1 To 35
Print n(m);
Next
Print
Print "600th number ";n(600)
For z As Long=0 To 9
Print "first umprimable number ending in ";z; " is ";endings(Str(z),n())
Next z
sleep
</syntaxhighlight>
<pre>
First 35
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 number 5242
first umprimable number ending in 0 is 200
first umprimable number ending in 1 is 595631
first umprimable number ending in 2 is 322
first umprimable number ending in 3 is 1203623
first umprimable number ending in 4 is 204
first umprimable number ending in 5 is 325
first umprimable number ending in 6 is 206
first umprimable number ending in 7 is 872897
first umprimable number ending in 8 is 208
first umprimable number ending in 9 is 212159
</pre>
 
=={{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 496 ⟶ 984:
fmt.Printf(" %d is: %9s\n", i, commatize(firstNum[i]))
}
}</langsyntaxhighlight>
 
{{out}}
Line 517 ⟶ 1,005:
</pre>
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Lens ((.~), ix, (&))
import Data.Numbers.Primes (isPrime)
import Data.List (find, intercalate)
Line 547 ⟶ 1,035:
lowest n = do
x <- find (\x -> x `mod` 10 == n) unPrimeable
pure (n, thousands $ show x)</langsyntaxhighlight>
{{out}}
<pre>
Line 565 ⟶ 1,053:
Lowest unprimeable number ending with 8: 208
Lowest unprimeable number ending with 9: 212,159
</pre>
 
=={{header|J}}==
Reshaping data is a strength of j.
<syntaxhighlight 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 (}.\.)
NB. under the antibase 10 representation (10&#.inv)
replace=: ([: ; <@}:\ ,"1 L:_1 ([: < (i.10) ,"0 1 }.)\.)&.(10&#.inv)
 
NB. primable tests if one of the replacements is prime
primable=: (1 e. 1 p: replace)&>
 
unprimable=: -.@:primable
 
assert 0 1 -: unprimable 193 200
</syntaxhighlight>
<pre>
NB. test 2e6 integers for unprimability
A=: unprimable i. 2e6
 
UNPRIMABLE=: I. A NB. indexes of the 1s are the unprimable numbers
 
35 {. UNPRIMABLE NB. first 35
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
 
UNPRIMABLE {~ <: 600 NB. 600th
5242
 
NB. the first unprimable numbers ending with 0--9
NB. sorted by least significant digit after in-order grouping by the LSD key.
(/: 10&|) ({./.~ 10&|) UNPRIMABLE
200 595631 322 1203623 204 325 206 872897 208 212159
</pre>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
public class UnprimeableNumbers {
 
Line 664 ⟶ 1,185:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 685 ⟶ 1,206:
9 is 212,159
</pre>
 
=={{header|JavaScript}}==
Auxiliary function:
<syntaxhighlight lang="javascript">
Number.prototype.isPrime = function() {
let i = 2, num = this;
if (num == 0 || num == 1) return false;
if (num == 2) return true;
while (i <= Math.ceil(Math.sqrt(num))) {
if (num % i == 0) return false;
i++;
}
return true;
}
</syntaxhighlight>
 
Core function:
<syntaxhighlight lang="javascript">
function isUnprimable(num) {
if (num < 100 || num.isPrime()) return false;
let arr = num.toString().split('');
for (let x = 0; x < arr.length; x++) {
let lft = arr.slice(0, x),
rgt = arr.slice(x + 1);
for (let y = 0; y < 10; y++) {
let test = lft.join('') + y.toString() + rgt.join('');
if (parseInt(test).isPrime()) return false;
}
}
return true;
}
</syntaxhighlight>
 
Main function for output:
<syntaxhighlight lang="javascript">
let unprimeables = [],
endings = new Array(10).fill('-'),
c = 1;
function chkEnds(n) {
let e = n % 10;
if (endings[e] == '-') endings[e] = n;
}
console.time('I');
while (unprimeables.length < 1000) {
if (isUnprimable(c)) {
unprimeables.push(c);
chkEnds(c)
}
c++;
}
console.log('The first 35 unprimeables:');
console.log(unprimeables.slice(0,35).join(', '));
console.log(`The 600th unprimeable: ${unprimeables[599].toLocaleString('en')}`);
console.log(`The 1000th unprimable: ${unprimeables[999].toLocaleString('en')}`);
console.timeEnd('I');
console.time('II');
while (endings.includes('-')) {
c++;
if (isUnprimable(c)) chkEnds(c);
}
for (c = 0; c < endings.length; c++) {
console.log(`First unprimeable ending with ${c}: ${endings[c].toLocaleString('en')}`);
}
console.timeEnd('II');
</syntaxhighlight>
 
{{out}}
<pre>
The 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: 5,242
The 1000th unprimable: 8,158
I: 240ms - timer ended
 
First unprimeable ending with 0: 200
First unprimeable ending with 1: 595,631
First unprimeable ending with 2: 322
First unprimeable ending with 3: 1,203,623
First unprimeable ending with 4: 204
First unprimeable ending with 5: 325
First unprimeable ending with 6: 206
First unprimeable ending with 7: 872,897
First unprimeable ending with 8: 208
First unprimeable ending with 9: 212,159
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 711 ⟶ 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 730 ⟶ 1,405:
9 212,159
</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">private const val MAX = 10000000
private val primes = BooleanArray(MAX)
 
fun main() {
sieve()
println("First 35 unprimeable numbers:")
displayUnprimeableNumbers(35)
val n = 600
println()
println("The ${n}th unprimeable number = ${nthUnprimeableNumber(n)}")
println()
val lowest = genLowest()
println("Least unprimeable number that ends in:")
for (i in 0..9) {
println(" $i is ${lowest[i]}")
}
}
 
private fun genLowest(): IntArray {
val lowest = IntArray(10)
var count = 0
var test = 1
while (count < 10) {
test++
if (unPrimable(test) && lowest[test % 10] == 0) {
lowest[test % 10] = test
count++
}
}
return lowest
}
 
private fun nthUnprimeableNumber(maxCount: Int): Int {
var test = 1
var count = 0
var result = 0
while (count < maxCount) {
test++
if (unPrimable(test)) {
count++
result = test
}
}
return result
}
 
private fun displayUnprimeableNumbers(maxCount: Int) {
var test = 1
var count = 0
while (count < maxCount) {
test++
if (unPrimable(test)) {
count++
print("$test ")
}
}
println()
}
 
private fun unPrimable(test: Int): Boolean {
if (primes[test]) {
return false
}
val s = test.toString() + ""
for (i in s.indices) {
for (j in 0..9) {
if (primes[replace(s, i, j).toInt()]) {
return false
}
}
}
return true
}
 
private fun replace(str: String, position: Int, value: Int): String {
val sChar = str.toCharArray()
sChar[position] = value.toChar()
return str.substring(0, position) + value + str.substring(position + 1)
}
 
private fun sieve() {
// primes
for (i in 2 until MAX) {
primes[i] = true
}
for (i in 2 until MAX) {
if (primes[i]) {
var j = 2 * i
while (j < MAX) {
primes[j] = false
j += i
}
}
}
}</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
 
The 600th unprimeable number = 5242
 
Least unprimeable number that ends in:
0 is 200
1 is 595631
2 is 322
3 is 1203623
4 is 204
5 is 325
6 is 206
7 is 872897
8 is 208
9 is 212159</pre>
 
=={{header|Lua}}==
<syntaxhighlight 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
table.firstn = function(t,n) local s=T{} n=n>#t and #t or n for i = 1,n do s[i]=t[i] end return s end
 
-- SIEVE:
local sieve, S = {}, 10000000
for i = 2,S do sieve[i]=true end
for i = 2,S do if sieve[i] then for j=i*i,S,i do sieve[j]=nil end end end
 
-- UNPRIMABLE:
local unprimables, lowests = T{}, T{}
local floor, log10 = math.floor, math.log10
 
local function unprimable(n)
if sieve[n] then return false end
local nd = floor(log10(n))+1
for i = 1, nd do
local pow10 = 10^(nd-i)
for j = 0, 9 do
local p = (floor(n/10/pow10) * 10 + j) * pow10 + (n % pow10)
if sieve[p] then return false end
end
end
return true
end
 
local n, done = 1, 0
while done < 10 do
if unprimable(n) then
unprimables:insert(n)
if not lowests[n%10] then
lowests[n%10] = n
done = done + 1
end
end
n = n + 1
end
 
-- OUTPUT:
local function commafy(i) return tostring(i):reverse():gsub("(%d%d%d)","%1,"):reverse():gsub("^,","") end
print("The first 35 unprimable numbers are:")
print(unprimables:firstn(35):concat(" "))
print()
print("The 600th unprimable number is: " .. commafy(unprimables[600]))
print()
print("The lowest unprimable number that ends in..")
for i = 0, 9 do
print(" " .. i .. " is: " .. commafy(lowests[i]))
end</syntaxhighlight>
{{out}}
<pre>The first 35 unprimable numbers are:
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 unprimable number is: 5,242
 
The lowest unprimable number that ends in..
0 is: 200
1 is: 595,631
2 is: 322
3 is: 1,203,623
4 is: 204
5 is: 325
6 is: 206
7 is: 872,897
8 is: 208
9 is: 212,159</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[Unprimeable]
Unprimeable[in_Integer] := Module[{id, new, pos},
id = IntegerDigits[in];
pos = Catenate@Table[
Table[
new = id;
new[[d]] = n;
new
,
{n, 0, 9}
]
,
{d, Length[id]}
];
pos //= Map[FromDigits];
NoneTrue[pos, PrimeQ]
]
res = {};
PrintTemporary[Dynamic[{Length[res], i}]];
i = 0;
While[Length[res] < 600,
If[Unprimeable[i],
AppendTo[res, i]
];
i++
];
 
PrintTemporary[Dynamic[{lastdig, i}]];
out = Table[
i = lastdig;
While[! Unprimeable[i],
i += 10
];
i
,
{lastdig, 0, 9}
];
 
res[[;; 35]]
res[[600]]
 
lastdigit = IntegerDigits /* Last;
Print["Least unprimeable number ending in ", lastdigit[#], ": ", #] & /@ SortBy[out, lastdigit];</syntaxhighlight>
{{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}
5242
Least unprimeable number ending in 0: 200
Least unprimeable number ending in 1: 595631
Least unprimeable number ending in 2: 322
Least unprimeable number ending in 3: 1203623
Least unprimeable number ending in 4: 204
Least unprimeable number ending in 5: 325
Least unprimeable number ending in 6: 206
Least unprimeable number ending in 7: 872897
Least unprimeable number ending in 8: 208
Least unprimeable number ending in 9: 212159</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import strutils
 
const N = 10_000_000
 
# Erastosthenes sieve.
var composite: array[0..N, bool] # Defualt is false i.e. composite.
composite[0] = true
composite[1] = true
for n in 2..N:
if not composite[n]:
for k in countup(n * n, N, n):
composite[k] = true
 
template isPrime(n: int): bool = not composite[n]
 
 
proc isUmprimeable(n: Positive): bool =
if n.isPrime: return false
var nd = $n
for i, prevDigit in nd:
for newDigit in '0'..'9':
if newDigit != prevDigit:
nd[i] = newDigit
if nd.parseInt.isPrime: return false
nd[i] = prevDigit # Restore initial digit.
result = true
 
 
echo "First 35 unprimeable numbers:"
var n = 100
var list: seq[int]
while list.len < 35:
if n.isUmprimeable:
list.add n
inc n
echo list.join(" "), '\n'
 
var count = 0
n = 199
while count != 600:
inc n
if n.isUmprimeable: inc count
echo "600th unprimeable number: ", ($n).insertSep(','), '\n'
 
for d in 0..9:
var n = 200 + d
while not n.isUmprimeable:
inc n, 10
echo "Lowest unprimeable number ending in ", d, " is ", ($n).insertSep(',')</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
 
Lowest unprimeable number ending in 0 is 200
Lowest unprimeable number ending in 1 is 595,631
Lowest unprimeable number ending in 2 is 322
Lowest unprimeable number ending in 3 is 1,203,623
Lowest unprimeable number ending in 4 is 204
Lowest unprimeable number ending in 5 is 325
Lowest unprimeable number ending in 6 is 206
Lowest unprimeable number ending in 7 is 872,897
Lowest unprimeable number ending in 8 is 208
Lowest unprimeable number ending in 9 is 212,159</pre>
 
=={{header|Pascal}}==
{{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}
 
const
base = 10;
 
type
TNumVal = array[0..base-1] of NativeUint;
Line 747 ⟶ 1,732:
MaxIdx : NativeUint;
end;
 
var //global
PotBase,
Line 753 ⟶ 1,738:
TotalCnt,
EndDgtCnt :NativeUint;
 
procedure Init;
var
Line 768 ⟶ 1,753:
EndDgtCnt := 0;
end;
 
Procedure ConvertNum(n: NativeUint;var NConv:TConvNum);
//extract digit position replace by "0" to get NumRest
Line 791 ⟶ 1,776:
end;
end;
 
procedure CheckOutPut(n: NativeUint);
Begin
Line 805 ⟶ 1,790:
end;
end;
 
function isPrime(n : NativeUint):boolean;inline;
var
Line 821 ⟶ 1,806:
if n mod p = 0 then
Exit;
inc(p += ,2);
if n mod p = 0 then
Exit;
inc(p += ,4);
end;
result := true;
end;
 
procedure InsertFound(LowDgt,n:NativeUInt);
Begin
Line 838 ⟶ 1,823:
end;
end;
 
function CheckUnprimable(n:NativeInt):boolean;
var
Line 854 ⟶ 1,839:
EXIT;
dgt := LowDgt;
 
result := true;
i := MaxIdx;
Line 866 ⟶ 1,851:
end;
end;
 
result := false;
For i := MaxIdx downto 1 do
Line 883 ⟶ 1,868:
end;
end;
 
function CheckUnprimableReduced(n:NativeInt):boolean;
//lowest digit already tested before
Line 904 ⟶ 1,889:
end;
end;
 
result := false;
For i := i downto 1 do
Line 921 ⟶ 1,906:
end;
end;
 
var
n,i : NativeUint;
Line 944 ⟶ 1,929:
writeln;
writeln('There are ',TotalCnt,' unprimable numbers upto ',n);
{$IFNDEF UNIX}readln;{$ENDIF}
end.</lang>
end.</syntaxhighlight>
{{out}}
<pre style="font-size:84%">
Line 967 ⟶ 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 1,383 ⟶ 2,369:
writeln('There are ',TotalCnt,' unprimable numbers upto ',n);
setlength(Primes,0);
end.</langsyntaxhighlight>
{{out}}
<pre style="height:35ex">
Line 1,455 ⟶ 2,441:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,484 ⟶ 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 1,504 ⟶ 2,490:
=={{header|Phix}}==
{{trans|Go}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>printf(1,"The first 35 unprimeable numbers are:\n")
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer count := 0, -- counts all unprimeable numbers
<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>
countFirst = 0,
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- counts all unprimeable numbers</span>
i = 100
<span style="color: #000000;">countFirst</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
sequence firstNum = repeat(0,10) -- stores the first unprimeable number ending with each digit
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span>
atom t1 = time()+1
<span style="color: #004080;">sequence</span> <span style="color: #000000;">firstNum</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- stores the first unprimeable number ending with each digit</span>
while countFirst<10 do
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">t1</span><span style="color: #0000FF;">=</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
if not is_prime(i) then -- unprimeable number must be composite
<span style="color: #008080;">while</span> <span style="color: #000000;">countFirst</span><span style="color: #0000FF;"><</span><span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
string s = sprintf("%d",i), b = s
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- unprimeable number must be composite</span>
integer le := length(s)
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span>
bool primeable = false
<span style="color: #004080;">integer</span> <span style="color: #000000;">le</span> <span style="color: #0000FF;">:=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
for j=1 to le do
<span style="color: #004080;">bool</span> <span style="color: #000000;">primeable</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
for k='0' to '9' do
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">le</span> <span style="color: #008080;">do</span>
if s[j]!=k then
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">to</span> <span style="color: #008000;">'9'</span> <span style="color: #008080;">do</span>
b[j] = k
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
integer n = to_integer(b)
<span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k</span>
if is_prime(n) then
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">to_integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
primeable = true
<span style="color: #008080;">if</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
exit
<span style="color: #000000;">primeable</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
end if
end if <span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if primeable then exit end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
b[j] = s[j] -- restore j'th digit to what it was originally
<span style="color: #008080;">if</span> <span style="color: #000000;">primeable</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- restore j'th digit to what it was originally</span>
if not primeable then
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
integer lastDigit = s[le]-'0'+1
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">primeable</span> <span style="color: #008080;">then</span>
if firstNum[lastDigit] == 0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">lastDigit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">le</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
firstNum[lastDigit] = i
<span style="color: #008080;">if</span> <span style="color: #000000;">firstNum</span><span style="color: #0000FF;">[</span><span style="color: #000000;">lastDigit</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
countFirst += 1
<span style="color: #000000;">firstNum</span><span style="color: #0000FF;">[</span><span style="color: #000000;">lastDigit</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
end if
<span style="color: #000000;">countFirst</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
count += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if count <= 35 then
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
printf(1,"%d ", i)
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">35</span> <span style="color: #008080;">then</span>
elsif count == 600 then
<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;">"%d "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
printf(1,"\n\nThe 600th unprimeable number is: %,d\n\n",i)
<span style="color: #008080;">elsif</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">600</span> <span style="color: #008080;">then</span>
end if
<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;">"\n\nThe 600th unprimeable number is: %,d\n\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if time()>t1 then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"checking %d, %d `endswiths` found\r",{i,countFirst})
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span>
t1 = time()+1
<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;">"checking %d, %d `endswiths` found\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">countFirst</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
i += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<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>
printf(1,"The first unprimeable number that ends in:\n")
for i=1 to 10 do
<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 unprimeable number that ends in:\n"</span><span style="color: #0000FF;">)</span>
printf(1," %d is: %,9d\n", {i-1, firstNum[i]})
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
end for</lang>
<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;">" %d is: %,9d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">firstNum</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,576 ⟶ 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 1,615 ⟶ 2,605:
write(" %s is: %9d\n", e, first_enders[e]);
}
}</langsyntaxhighlight>
Output:
<pre>
Line 1,636 ⟶ 2,626:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import count, islice
 
def primes(_cache=[2, 3]):
Line 1,687 ⟶ 2,677:
 
for i,v in enumerate(first):
print(f'{i} ending: {v}')</langsyntaxhighlight>
{{out}}
<pre>First 35:
Line 1,706 ⟶ 2,696:
9 ending: 212159
</pre>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
 
(define cached-prime?
(let ((hsh# (make-hash))) (λ (p) (hash-ref! hsh# p (λ () (prime? p))))))
 
(define (zero-digit n d)
(define p (expt 10 d))
(+ (remainder n p) (* 10 p (quotient n (* p 10)))))
 
(define (primeable? n)
(or (cached-prime? n)
(for*/first ((d (in-range (add1 (order-of-magnitude n))))
(n0 (in-value (zero-digit n d)))
(p (in-value (expt 10 d)))
(r (in-range 10))
(n+ (in-value (+ n0 (* r p))))
#:when (cached-prime? n+))
n+)))
 
(define unprimeable? (negate primeable?))
 
(module+
main
(printf "First 35 unprimeable numbers: ~a~%"
(let recur ((i 0) (n 1) (acc null))
(cond [(= i 35) (reverse acc)]
[(unprimeable? n) (recur (add1 i) (add1 n) (cons n acc))]
[else (recur i (add1 n) acc)])))
 
(printf "600th unprimeable number: ~a~%"
(let recur ((i 0) (n 1) (u #f))
(cond [(= i 600) u]
[(unprimeable? n) (recur (add1 i) (add1 n) n)]
[else (recur i (add1 n) u)])))
 
(for ((d 10))
(printf "Least unprimeable number ending in ~a = ~a~%" d
(for/first ((i (in-range (+ 100 d) +Inf.0 10)) #:when (unprimeable? i)) i))))
 
(module+ test
(require rackunit)
(check-equal? (zero-digit 1234 2) 1034)
(check-equal? (primeable? 10) 11)
(check-true (unprimeable? 200))
(check-false (unprimeable? 201)))</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
Least unprimeable number ending in 0 = 200
Least unprimeable number ending in 1 = 595631
Least unprimeable number ending in 2 = 322
Least unprimeable number ending in 3 = 1203623
Least unprimeable number ending in 4 = 204
Least unprimeable number ending in 5 = 325
Least unprimeable number ending in 6 = 206
Least unprimeable number ending in 7 = 872897
Least unprimeable number ending in 8 = 208
Least unprimeable number ending in 9 = 212159</pre>
 
=={{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 1,719 ⟶ 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 1,737 ⟶ 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 1,757 ⟶ 2,811:
=={{header|REXX}}==
Some effort was put into the optimization of the generation of primes &nbsp; (the &nbsp; '''genP''' &nbsp; subroutine).
 
<lang rexx>/*REXX program finds and displays unprimeable numbers (non─negative integers). */
With the addition of the computation of squared primes, &nbsp; the program now is about '''4''' times faster.
<syntaxhighlight 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.*/
if x=='' | x=="," then x= 600 /* " " " " " " */
if hp=='' | hp=="," then hp= 10000000 /* " " " " " " */
edsu=4; 0 ed.1= 1; ed.2= 3; ed.3= 7; ed.4= 9 /*thenumber "end"of digitsunprimeable whichnumbers areso prime; #>9far.*/
eds=4; ed.1= 1; ed.2= 3; ed.3= 7; ed.4= 9 /*"end" digits which are prime; prime>9*/
call genP hp /*generate primes up to & including HP.*/
# = 0 /*number of unprimeable numbers so far.*/
$$=; $.=. /*a list " " " " " */
/*1─ and 2─digit #'s are all primeable.*/
do j=100; if !.j then iterate /*Prime? Unprimeable must be composite*/
LLm= length(j) /*obtain the length-1 of the number J. */
meat= left(j, L-1Lm) /*obtain the first L-1Lm digits of J. */
/* [↑] examine the "end" digit of J. */
do e_=1 for eds; new= meat || ed.e_ /*obtain a different number (than J).*/
Line 1,780 ⟶ 2,835:
if !.new then iterate j /*This new number a prime? " " */
end /*f_*/ /* [↑] examine the front digit of J. */
do a_= 2 tofor LLm-1 /*traipse through the middle digits. */
meat= left(j, a_ - 1) /*use a number of left─most dec. digits*/
rest= substr(j, a_ + 1) /* " " " " right─most " " */
Line 1,789 ⟶ 2,844:
end /*n_*/
end /*a_*/
#u= #u + 1 /*bump the count of unprimeable numbers*/
if #u<=n then $$= $$ commas(j) /*maybe add unprimeable # to $$ list.*/
if #u==x then $.ox= commas(j) /*assign the Xth unprimeable number.*/
parse var j '' -1 _ /*obtain the right─most dec digit of J.*/
if $._==. then $._= j /*the 1st unprimeable # that ends in _.*/
Line 1,812 ⟶ 2,867:
th:procedure;parse arg x;return x||word('th st nd rd',1+(x//10)*(x//100%10\==1)*(x//10<4))
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6= 13; @.7=17; @.8=19; @.9=23; @.10=29; #p@.11=1031
!.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1; !.19=1; !.23=1; !.29=1
#= 11; sq.#= @.# **2
do lim=100 until lim*lim>=hp /*only keep primes up to the sqrt(hp). */
end /*lim*/ /* [↑] find limit for storing primes. */
do j=@.#p+2 by 2 to hp; parse var j '' -1 _; if _==5 then iterate /*only find odd primes from here on. ÷ by 5?*/
parse varif j// ''3==0 -1 _then iterate; if _j// 7==50 then iterate /*Get last digit; is last digitif aj//11==0 "5" ?*/then iterate
if j// 313==0 then iterate; if j//17==0 then iterate; if j/*is/19==0 J divisiblethen by 3? Then not prime*/iterate
if j// 723==0 then iterate; if j//*29==0 " "then " " 7? " " " */iterate
if j// do k=11==0 thenwhile iteratesq.k<=j /*divide " " " " 11? " " by some generated "odd primes. */
if j//13@.k==0 then iterate j /*Is "J "divisible by " " 13P? Then not " " " prime*/
if j//17==0 then iterate /* " " " " 17? " " " */
if j//19==0 then iterate /* " " " " 19? " " " */
if j//23==0 then iterate /* " " " " 23? " " " */
if j//29==0 then iterate /* " " " " 29? " " " */
do k=11 while k*k<=j /*divide by some generated odd primes. */
if j // @.k==0 then iterate j /*Is J divisible by P? Then not prime*/
end /*k*/ /* [↓] a prime (J) has been found. */
#p= #p+1; if #p<=lim then @.#p=j; !.j=1 /*bump prime count; assign prime to @. */
end /* sq.#= j*/;j return /*calculate square of J for fast WHILE.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
 
Line 1,851 ⟶ 2,901:
the first unprimeable number that ends in 8 is: 208
the first unprimeable number that ends in 9 is: 212,159
</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++}}
<syntaxhighlight lang="rust">// main.rs
mod bit_array;
mod prime_sieve;
 
use prime_sieve::PrimeSieve;
 
// return number of decimal digits
fn count_digits(mut n: u32) -> u32 {
let mut digits = 0;
while n > 0 {
n /= 10;
digits += 1;
}
digits
}
 
// return the number with one digit replaced
fn change_digit(mut n: u32, mut index: u32, new_digit: u32) -> u32 {
let mut p = 1;
let mut changed = 0;
while index > 0 {
changed += p * (n % 10);
p *= 10;
n /= 10;
index -= 1;
}
changed += (10 * (n / 10) + new_digit) * p;
changed
}
 
fn unprimeable(sieve: &PrimeSieve, n: u32) -> bool {
if sieve.is_prime(n as usize) {
return false;
}
let d = count_digits(n);
for i in 0..d {
for j in 0..10 {
let m = change_digit(n, i, j);
if m != n && sieve.is_prime(m as usize) {
return false;
}
}
}
true
}
 
fn main() {
let mut count = 0;
let mut n = 100;
let mut lowest = vec![0; 10];
let mut found = 0;
let sieve = PrimeSieve::new(10000000);
println!("First 35 unprimeable numbers:");
while count < 600 || found < 10 {
if unprimeable(&sieve, n) {
if count < 35 {
if count > 0 {
print!(", ");
}
print!("{}", n);
}
count += 1;
if count == 600 {
println!("\n600th unprimeable number: {}", n);
}
let last_digit = n as usize % 10;
if lowest[last_digit] == 0 {
lowest[last_digit] = n;
found += 1;
}
}
n += 1;
}
for i in 0..10 {
println!("Least unprimeable number ending in {}: {}", i, lowest[i]);
}
}</syntaxhighlight>
 
<syntaxhighlight lang="rust">// prime_sieve.rs
use crate::bit_array;
 
pub struct PrimeSieve {
composite: bit_array::BitArray,
}
 
impl PrimeSieve {
pub fn new(limit: usize) -> PrimeSieve {
let mut sieve = PrimeSieve {
composite: bit_array::BitArray::new(limit / 2),
};
let mut p = 3;
while p * p <= limit {
if !sieve.composite.get(p / 2 - 1) {
let inc = p * 2;
let mut q = p * p;
while q <= limit {
sieve.composite.set(q / 2 - 1, true);
q += inc;
}
}
p += 2;
}
sieve
}
pub fn is_prime(&self, n: usize) -> bool {
if n < 2 {
return false;
}
if n % 2 == 0 {
return n == 2;
}
!self.composite.get(n / 2 - 1)
}
}</syntaxhighlight>
 
<syntaxhighlight lang="rust">// bit_array.rs
pub struct BitArray {
array: Vec<u32>,
}
 
impl BitArray {
pub fn new(size: usize) -> BitArray {
BitArray {
array: vec![0; (size + 31) / 32],
}
}
pub fn get(&self, index: usize) -> bool {
let bit = 1 << (index & 31);
(self.array[index >> 5] & bit) != 0
}
pub fn set(&mut self, index: usize, new_val: bool) {
let bit = 1 << (index & 31);
if new_val {
self.array[index >> 5] |= bit;
} else {
self.array[index >> 5] &= !bit;
}
}
}</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
Least unprimeable number ending in 0: 200
Least unprimeable number ending in 1: 595631
Least unprimeable number ending in 2: 322
Least unprimeable number ending in 3: 1203623
Least unprimeable number ending in 4: 204
Least unprimeable number ending in 5: 325
Least unprimeable number ending in 6: 206
Least unprimeable number ending in 7: 872897
Least unprimeable number ending in 8: 208
Least unprimeable number ending in 9: 212159
</pre>
 
=={{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 1,885 ⟶ 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 1,903 ⟶ 3,156:
First unprimeable that ends with 8: 208
First unprimeable that ends with 9: 212159
</pre>
 
=={{header|Swift}}==
{{trans|Rust}}
<syntaxhighlight lang="swift">import Foundation
 
class BitArray {
var array: [UInt32]
 
init(size: Int) {
array = Array(repeating: 0, count: (size + 31)/32)
}
func get(index: Int) -> Bool {
let bit = UInt32(1) << (index & 31)
return (array[index >> 5] & bit) != 0
}
func set(index: Int, value: Bool) {
let bit = UInt32(1) << (index & 31)
if value {
array[index >> 5] |= bit
} else {
array[index >> 5] &= ~bit
}
}
}
 
class PrimeSieve {
let composite: BitArray
init(size: Int) {
composite = BitArray(size: size/2)
var p = 3
while p * p <= size {
if !composite.get(index: p/2 - 1) {
let inc = p * 2
var q = p * p
while q <= size {
composite.set(index: q/2 - 1, value: true)
q += inc
}
}
p += 2
}
}
func isPrime(number: Int) -> Bool {
if number < 2 {
return false
}
if (number & 1) == 0 {
return number == 2
}
return !composite.get(index: number/2 - 1)
}
}
 
// return number of decimal digits
func countDigits(number: Int) -> Int {
var digits = 0
var n = number
while n > 0 {
n /= 10
digits += 1
}
return digits
}
 
// return the number with one digit replaced
func changeDigit(number: Int, index: Int, digit: Int) -> Int {
var p = 1
var changed = 0
var n = number
var i = index
while i > 0 {
changed += p * (n % 10)
p *= 10
n /= 10
i -= 1
}
changed += (10 * (n / 10) + digit) * p
return changed
}
 
func unprimeable(sieve: PrimeSieve, number: Int) -> Bool {
if sieve.isPrime(number: number) {
return false
}
for i in 0..<countDigits(number: number) {
for j in 0..<10 {
let n = changeDigit(number: number, index: i, digit: j)
if n != number && sieve.isPrime(number: n) {
return false
}
}
}
return true
}
 
var count = 0
var n = 100
var lowest = Array(repeating: 0, count: 10)
var found = 0
let sieve = PrimeSieve(size: 10000000)
print("First 35 unprimeable numbers:")
while count < 600 || found < 10 {
if unprimeable(sieve: sieve, number: n) {
if count < 35 {
if count > 0 {
print(", ", terminator: "")
}
print(n, terminator: "")
}
count += 1
if count == 600 {
print("\n600th unprimeable number: \(n)")
}
let lastDigit = n % 10
if lowest[lastDigit] == 0 {
lowest[lastDigit] = n
found += 1
}
}
n += 1
}
for i in 0..<10 {
let number = NSNumber(value: lowest[i])
let str = NumberFormatter.localizedString(from: number, number: .decimal)
print("Least unprimeable number ending in \(i): \(str)")
}</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
Least unprimeable number ending in 0: 200
Least unprimeable number ending in 1: 595,631
Least unprimeable number ending in 2: 322
Least unprimeable number ending in 3: 1,203,623
Least unprimeable number ending in 4: 204
Least unprimeable number ending in 5: 325
Least unprimeable number ending in 6: 206
Least unprimeable number ending in 7: 872,897
Least unprimeable number ending in 8: 208
Least unprimeable number ending in 9: 212,159
</pre>
 
Line 1,909 ⟶ 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 1,954 ⟶ 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 1,974 ⟶ 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 1,979 ⟶ 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 1,994 ⟶ 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 2,002 ⟶ 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