Primes with digits in nondecreasing order: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(28 intermediate revisions by 19 users not shown)
Line 3:
 
;Task:
Find all primes   ('''n''')   primes with their decimal digits in non-decreasing order,   where   '''n   <   10001,000'''
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
I a < 2 | a % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(a))).step(2)
I a % i == 0
R 0B
R 1B
 
F is_non_decreasing(=n)
V prev = 10
L n != 0
V d = n % 10
I d > prev {R 0B}
prev = d
n I/= 10
R 1B
 
V c = 0
L(n) 1000
I is_non_decreasing(n) & is_prime(n)
c++
print(‘#3’.format(n), end' I c % 10 == 0 {"\n"} E ‘ ’)
print()
print(‘Found ’c‘ primes with digits in nondecreasing order’)</syntaxhighlight>
 
{{out}}
<pre>
2 3 5 7 11 13 17 19 23 29
37 47 59 67 79 89 113 127 137 139
149 157 167 179 199 223 227 229 233 239
257 269 277 337 347 349 359 367 379 389
449 457 467 479 499 557 569 577 599 677
 
Found 50 primes with digits in nondecreasing order
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC NonDecreasingDigits(INT x)
BYTE c,d,last
 
c=0 last=9
WHILE x#0
DO
d=x MOD 10
IF d>last THEN
RETURN (0)
FI
last=d
x==/10
OD
RETURN (1)
 
PROC Main()
DEFINE MAX="999"
BYTE ARRAY primes(MAX+1)
INT i,count=[0]
 
Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
FOR i=2 TO MAX
DO
IF primes(i)=1 AND NonDecreasingDigits(i)=1 THEN
PrintI(i) Put(32)
count==+1
FI
OD
PrintF("%E%EThere are %I primes",count)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Primes_with_digits_in_nondecreasing_order.png Screenshot from Atari 8-bit computer]
<pre>
2 3 5 7 11 13 17 19 23 29 37 47 59 67 79 89 113 127 137 139 149 157 167 179 199 223 227 229
233 239 257 269 277 337 347 349 359 367 379 389 449 457 467 479 499 557 569 577 599 677
 
There are 50 primes
</pre>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find primes where the digits are non-descending #
<syntaxhighlight lang="algol68">BEGIN # find primes where the digits are non-descending #
INT max number = 1000;
# sieve the primes to max number #
PR read "primes.incl.a68" PR
[ 1 : max number ]BOOL prime;
prime[ 1 ] := FALSE;BOOL prime[ 2= ]PRIMESIEVE :=max TRUEnumber;
FOR i FROM 3 BY 2 TO max number DO prime[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO max number DO prime[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( max number ) DO
IF prime[ i ] THEN
FOR s FROM i * i BY i + i TO max number DO prime[ s ] := FALSE OD
FI
OD;
# we can easily generate candidate numbers with a few nested loops #
INT p count := 0;
Line 50 ⟶ 129:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 61 ⟶ 140:
Found 50 non-descending primes up to 1000
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">(⊢(/⍨)(∧/2≤/10(⊥⍣¯1)⊢)¨)∘(⊢(/⍨)(2=0+.=⍳|⊢)¨)⍳1000</syntaxhighlight>
{{out}}
<pre>2 3 5 7 11 13 17 19 23 29 37 47 59 67 79 89 113 127 137 139 149 157 167
179 199 223 227 229 233 239 257 269 277 337 347 349 359 367 379
389 449 457 467 479 499 557 569 577 599 677</pre>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">primes: select 1..1000 => prime?
nondecreasing?: function [n][
ds: digits n
Line 78 ⟶ 166:
 
loop split.every: 10 select primes => nondecreasing? 'a ->
print map a => [pad to :string & 4]</langsyntaxhighlight>
 
{{out}}
Line 89 ⟶ 177:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRIMES_WITH_DIGITS_IN_NONDECREASING_ORDER.AWK
BEGIN {
Line 121 ⟶ 209:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 132 ⟶ 220:
Primes with digits in nondecreasing order 1-1000: 50
</pre>
 
=={{header|BASIC}}==
{{works with|QBasic}}
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 DIM P(1000)
30 P(0)=-1: P(1)=-1
40 FOR I=2 TO SQR(1000)
50 IF P(I)=0 THEN FOR J=I+I TO 1000 STEP I: P(J)=-1: NEXT
60 NEXT
70 FOR A=0 TO 9: FOR B=A TO 9: FOR C=B TO 9
80 N=A*100+B*10+C
90 IF P(N)=0 THEN PRINT N,
100 NEXT C,B,A</syntaxhighlight>
{{out}}
<pre> 2 3 5 7 11
13 17 19 23 29
37 47 59 67 79
89 113 127 137 139
149 157 167 179 199
223 227 229 233 239
257 269 277 337 347
349 359 367 379 389
449 457 467 479 499
557 569 577 599 677</pre>
 
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 3 = 0 then return v = 3
d = 5
while d * d <= v
if v mod d = 0 then return False else d += 2
end while
return True
end function
 
function is_ndp(n)
d = 10
do
ld = d
d = n mod 10
if d > ld then return False
n /= 10
until n = 0
return True
end function
 
c = 0
print "Primes below 1,000 with digits in non-decreasing order:"
for i = 2 to 999
if isPrime(i) and is_ndp(i) then
c += 1
print i; chr(9);
if c mod 10 = 0 then print
end if
next i
print chr(10); c; " such primes found."
end</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
ElseIf v % 2 = 0 : ProcedureReturn #False
ElseIf v < 9 : ProcedureReturn #True
ElseIf v % 3 = 0 : ProcedureReturn #False
Else
Protected r = Round(Sqr(v), #PB_Round_Down)
Protected f = 5
While f <= r
If v % f = 0 Or v % (f + 2) = 0
ProcedureReturn #False
EndIf
f + 6
Wend
EndIf
ProcedureReturn #True
EndProcedure
 
Procedure is_ndp(n.i)
d.i = 10
Repeat
ld.i = d
d = n % 10
If d > ld
ProcedureReturn #False
EndIf
n / 10
Until n = 0
ProcedureReturn #True
EndProcedure
 
OpenConsole()
c.i = 0
PrintN("Primes below 1,000 with digits in non-decreasing order:")
For i.i = 2 To 999
If isPrime(i) And is_ndp(i)
c + 1
Print(Str(i) + #TAB$)
If c % 10 = 0 : PrintN("") : EndIf
EndIf
Next i
PrintN(#CRLF$ + Str(c) + " such primes found."): Input()
CloseConsole()</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub isPrime(v)
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
if mod(v, 3) = 0 then return v = 3 : fi
d = 5
while d * d <= v
if mod(v, d) = 0 then return False else d = d + 2 : fi
wend
return True
end sub
 
sub is_ndp(n)
d = 10
repeat
ld = d
d = mod(n, 10)
if d > ld then return False : fi
n = int(n / 10)
until n = 0
return True
end sub
 
c = 0
print "Primes below 1,000 with digits in non-decreasing order:"
for i = 2 to 999
if isPrime(i) and is_ndp(i) then
c = c + 1
print i using "####";
if mod(c, 10) = 0 then print : fi
endif
next i
print chr$(10), c, " such primes found."
end</syntaxhighlight>
 
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr";
 
let sieve(prime, max) be
$( 0!prime := false
1!prime := false
for i=2 to max do i!prime := true
for i=2 to max/2
if i!prime
$( let j = i*2
while j <= max
$( j!prime := false
j := j + i
$)
$)
$)
 
let start() be
$( let prime = getvec(1000)
let c = 0
sieve(prime, 1000)
for d100 = 0 to 9
for d10 = d100 to 9
for d1 = d10 to 9
$( let n = d100*100 + d10*10 + d1
if n!prime then
$( writed(n,4)
c := c + 1
if c rem 10 = 0 then wrch('*N')
$)
$)
freevec(prime)
$)</syntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 19 23 29
37 47 59 67 79 89 113 127 137 139
149 157 167 179 199 223 227 229 233 239
257 269 277 337 347 349 359 367 379 389
449 457 467 479 499 557 569 577 599 677</pre>
 
=={{header|C#|CSharp}}==
The ''chars'' array explicitly enforces the case order, not relying on the language's idea of what letters are before or after each other.
<langsyntaxhighlight lang="csharp">using System.Linq; using System.Collections.Generic; using static System.Console; using static System.Math;
class Program {
Line 167 ⟶ 438:
if (!flags[j]) { yield return j;
for (int k = sq, i = j << 1; k <= lim; k += i) flags[k] = true; }
for (; j <= lim; j += 2) if (!flags[j]) yield return j; } }</langsyntaxhighlight>
{{out}}
<pre style="height:20em"> 11 111 11111 1111111
Line 242 ⟶ 513:
Eh Ep Ez FH FN Fb Ff Fl Fr Fz
Base 62: found 150 non-decreasing primes under G8</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
 
procedure GetDigits(N: integer; var IA: TIntegerDynArray);
{Get an array of the integers in a number}
var T: integer;
begin
SetLength(IA,0);
repeat
begin
T:=N mod 10;
N:=N div 10;
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=T;
end
until N<1;
end;
 
 
 
procedure NonDecreasingPrime(Memo: TMemo);
var I,Cnt: integer;
var IA: TIntegerDynArray;
var S: string;
 
function NotDecreasing(N: integer): boolean;
var I: integer;
begin
Result:=False;
GetDigits(N,IA);
for I:=0 to High(IA)-1 do
if IA[I]<IA[I+1] then exit;
Result:=True;
end;
 
begin
Cnt:=0;
S:='';
for I:=0 to 1000-1 do
if IsPrime(I) then
if NotDecreasing(I) then
begin
Inc(Cnt);
S:=S+Format('%5D',[I]);
If (Cnt mod 5)=0 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count = '+IntToStr(Cnt));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11
13 17 19 23 29
37 47 59 67 79
89 113 127 137 139
149 157 167 179 199
223 227 229 233 239
257 269 277 337 347
349 359 367 379 389
449 457 467 479 499
557 569 577 599 677
 
Count = 50
Elapsed Time: 3.008 ms.
</pre>
 
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Primes with digits in nondecreasing order: Nigel Galloway. May 16th., 2021
let rec fN g=function n when n<10->(n<=g) |n when (n%10)<=g->fN(n%10)(n/10) |_->false
let fN=fN 9 in primes32()|>Seq.takeWhile((>)1000)|>Seq.filter fN|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 256 ⟶ 622:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: grouping lists lists.lazy math math.primes.lists
present prettyprint ;
 
lprimes [ present [ <= ] monotonic? ] lfilter
[ 1000 < ] lwhile [ . ] leach</langsyntaxhighlight>
{{out}}
<pre style="height:14em">
Line 315 ⟶ 681:
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">echo (([:*./2<:/\10#.^:_1])"0#])@(i.&.(p:^:_1)) 1000
exit 0</syntaxhighlight>
{{out}}
<pre>2 3 5 7 11 13 17 19 23 29 37 47 59 67 79 89 113 127 137 139 149 157 167 179 199 223 227 229 233 239 257 269 277 337 347 349 359 367 379 389 449 457 467 479 499 557 569 577 599 677</pre>
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
function is_ndp( byval n as integer ) as boolean
Line 332 ⟶ 703:
for i as uinteger = 2 to 999
if isprime(i) andalso is_ndp(i) then print i;" ";
next i : print</langsyntaxhighlight>
{{out}}<pre>2 3 5 7 11 13 17 19 23 29 37 47 59 67 79 89 113 127 137 139 149 157 167 179 199 223 227 229 233 239 257 269 277 337 347 349 359 367 379 389 449 457 467 479 499 557 569 577 599 677</pre>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">for n = primes[2,1000]
if sort[integerDigits[n]] == integerDigits[n]
print["$n "]</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 19 23 29 37 47 59 67 79 89 113 127 137 139 149 157 167 179 199 223 227 229 233 239 257 269 277 337 347 349 359 367 379 389 449 457 467 479 499 557 569 577 599 677
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"rcu"
)
 
func nonDescending(p int) bool {
var digits []int
for p > 0 {
digits = append(digits, p%10)
p = p / 10
}
for i := 0; i < len(digits)-1; i++ {
if digits[i+1] > digits[i] {
return false
}
}
return true
}
 
func main() {
primes := rcu.Primes(999)
var nonDesc []int
for _, p := range primes {
if nonDescending(p) {
nonDesc = append(nonDesc, p)
}
}
fmt.Println("Primes below 1,000 with digits in non-decreasing order:")
for i, n := range nonDesc {
fmt.Printf("%3d ", n)
if (i+1)%10 == 0 {
fmt.Println()
}
}
fmt.Printf("\n%d such primes found.\n", len(nonDesc))
}</syntaxhighlight>
 
{{out}}
<pre>
Primes below 1,000 with digits in non-decreasing order:
2 3 5 7 11 13 17 19 23 29
37 47 59 67 79 89 113 127 137 139
149 157 167 179 199 223 227 229 233 239
257 269 277 337 347 349 359 367 379 389
449 457 467 479 499 557 569 577 599 677
 
50 such primes found.
</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 implementation of `is_prime`.
<syntaxhighlight lang="jq">def digits: tostring | explode;
 
# Input: an upper bound, or `infinite`
# Output: a stream
def primes_with_nondecreasing_digits:
(2, range(3; .; 2))
| select( (digits | (. == sort)) and is_prime);
 
1000 | primes_with_nondecreasing_digits</syntaxhighlight>
{{out}} (abbreviated)
<pre>
2
3
5
7
11
...
557
569
577
599
677
</pre>
 
=={{header|Julia}}==
{{trans|Raku}}
Note for the case-sensitive digits base 62 example: Julia defaults to 'A' < 'a' in sorting. So Aa is in order, but aA is not nondecreasing.
<langsyntaxhighlight lang="julia">using Primes
 
const range = 2:999
Line 348 ⟶ 811:
foreach(p -> print(lpad(string(p[2], base=base), 5), p[1] % 16 == 0 ? "\n" : ""), enumerate(primes))
end
</langsyntaxhighlight>{{out}}
<pre>
Base 2: 4 non-descending primes between 1 and 1111111:
Line 421 ⟶ 884:
DF DH DL DN DX Dl Dp Dr Dv EF EJ Ed Eh Ep Ez FH
FN Fb Ff Fl Fr Fz
</pre>
 
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
 
# Primes with digits in nondecreasing order
 
# # Variables:
#
integer MAXPRIME=1000 MINPRIME=2
 
# # Functions:
#
# # Function _isprime(n) return 1 for prime, 0 for not prime
#
function _isprime {
typeset _n ; integer _n=$1
typeset _i ; integer _i
 
(( _n < 2 )) && return 0
for (( _i=2 ; _i*_i<=_n ; _i++ )); do
(( ! ( _n % _i ) )) && return 0
done
return 1
}
 
 
# # Function _isnondecreasing(n) return 1 when digits nondecreasing
#
function _isnondecreasing {
typeset _n ; integer _n=$1
typeset _i ; integer _i
 
(( ${#_n} < 2 )) && return 1 # Always for single digit
for((_i=0; _i<${#_n}-1; _i++)); do
(( ${_n:${_i}:1} > ${_n:$((_i+1)):1} )) && return 0
done
return 1
}
 
######
# main #
######
 
integer i cnt=0
for ((i=MINPRIME; i<MAXPRIME; i++)); do
_isprime ${i} && (( ! $? )) && continue
_isnondecreasing ${i} && (( ! $? )) && continue
(( cnt++ )) && printf " %d" ${i}
done
printf "\n%d Primes with digits in nondecreasing order < %d\n" ${cnt} $MAXPRIME
</syntaxhighlight>
{{out}}<pre>
3 5 7 11 13 17 19 23 29 37 47 59 67 79 89 113 127 137 139 149 157 167 179 199 223 227 229 233 239 257 269 277 337 347 349 359 367 379 389 449 457 467 479 499 557 569 577 599 677
50 Primes with digits in nondecreasing order < 1000 </pre>
 
=={{header|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
BOOLEAN PRIME
DIMENSION PRIME(1000), COL(10)
PRIME(0) = 0B
PRIME(1) = 0B
SQMAX=SQRT.(1000)
THROUGH INIT, FOR I=2, 1, I.G.1000
INIT PRIME(I) = 1B
THROUGH SIEVE, FOR I=2, 1, I.G.SQMAX
WHENEVER PRIME(I)
THROUGH SIEVE2, FOR J=I+I, I, J.G.1000
SIEVE2 PRIME(J) = 0B
END OF CONDITIONAL
SIEVE CONTINUE
C = 1
THROUGH TEST, FOR D100=0, 1, D100.G.9
THROUGH TEST, FOR D10=D100, 1, D10.G.9
THROUGH TEST, FOR D1=D10, 1, D1.G.9
N = D100*100+D10*10+D1
WHENEVER PRIME(N)
COL(C) = N
C = C+1
WHENEVER C.G.10
PRINT FORMAT CFMT,COL(1),COL(2),COL(3),COL(4),
0 COL(5),COL(6),COL(7),COL(8),COL(9),COL(10)
C = 1
END OF CONDITIONAL
END OF CONDITIONAL
TEST CONTINUE
VECTOR VALUES CFMT = $10(I4)*$
END OF PROGRAM </syntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 19 23 29
37 47 59 67 79 89 113 127 137 139
149 157 167 179 199 223 227 229 233 239
257 269 277 337 347 349 359 367 379 389
449 457 467 479 499 557 569 577 599 677</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Partition[
Cases[Most@
NestWhileList[NextPrime,
2, # < 1000 &], _?(OrderedQ[IntegerDigits@#] &)],
UpTo[10]] // TableForm</syntaxhighlight>
 
{{out}}<pre>
2 3 5 7 11 13 17 19 23 29
37 47 59 67 79 89 113 127 137 139
149 157 167 179 199 223 227 229 233 239
257 269 277 337 347 349 359 367 379 389
449 457 467 479 499 557 569 577 599 677
</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat, sugar
 
func isPrime(n: Natural): bool =
Line 454 ⟶ 1,025:
echo &"Found {result.len} primes:"
for i, n in result:
stdout.write &"{n:3}", if (i + 1) mod 10 == 0: '\n' else: ' '</langsyntaxhighlight>
 
{{out}}
Line 465 ⟶ 1,036:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">nd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">nd</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;">"%d non-decreasing primes &lt; 1,000: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 476 ⟶ 1,047:
 
=={{header|Perl}}==
{{libheader|ntheory}}
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
use strict; # https://rosettacode.org/wiki/Primes_with_digits_in_nondecreasing_order
use warnings;
use ntheory qw( primes );
 
my @want = grep ! /(.)(.)(??{$1 > $2 ? '' : '(*FAIL)'})/, @{ primes(1000) };
my @primes = grep {
print "@want" =~ s/.{50}\K /\n/gr . "\n\ncount: " . @want . "\n";</syntaxhighlight>
! /(.)(.)(??{$1 > $2 ? '' : '(*FAIL)'})/ and (1 x $_) !~ /^(11+)\1+$/
} 2 .. 999;
print "@primes\n" =~ s/.{50}\K /\n/gr, "\ncount: " . @primes, "\n";</lang>
{{out}}
<pre>
Line 495 ⟶ 1,066:
</pre>
 
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">To run:
Start up.
Loop.
If a counter is past 1000, break.
If the counter is prime and non-decreasing, write the counter then " " on the console without advancing.
Repeat.
Wait for the escape key.
Shut down.
 
To decide if a number is non-decreasing:
Privatize the number.
Put 10 into a previous digit number.
Loop.
Divide the number by 10 giving a quotient and a remainder.
If the remainder is greater than the previous digit, say no.
Put the remainder into the previous digit.
Put the quotient into the number.
If the number is 0, say yes.
Repeat.
 
To decide if a number is prime and non-decreasing:
If the number is not prime, say no.
If the number is not non-decreasing, say no.
Say yes.</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 19 23 29 37 47 59 67 79 89 113 127 137 139 149 157 167 179 199 223 227 229 233 239 257 269 277 337 347 349 359 367 379 389 449 457 467 479 499 557 569 577 599 677
</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
 
PRINT$NUMBER: PROCEDURE (N);
DECLARE S (6) BYTE INITIAL ('.....$');
DECLARE (N, P) ADDRESS, C BASED P BYTE;
P = .S(5);
DIGIT:
P = P - 1;
C = N MOD 10 + '0';
N = N / 10;
IF N > 0 THEN GO TO DIGIT;
CALL PRINT(P);
END PRINT$NUMBER;
 
SIEVE: PROCEDURE (MAX, PBASE);
DECLARE (MAX, PBASE) ADDRESS, P BASED PBASE BYTE;
DECLARE (I, J) ADDRESS;
P(0)=0;
P(1)=0;
DO I=2 TO MAX; P(I)=1; END;
DO I=2 TO MAX/2;
IF P(I) THEN
DO J=I+I TO MAX BY I;
P(J)=0;
END;
END;
END SIEVE;
 
DECLARE (D$100, D$10, D$1, COL) BYTE;
DECLARE P (1000) BYTE;
DECLARE N ADDRESS;
CALL SIEVE(LAST(P), .P);
 
COL = 0;
DO D$100 = 0 TO 9;
DO D$10 = D$100 TO 9;
DO D$1 = D$10 TO 9;
N = D$100*100 + D$10*10 + D$1;
IF P(N) THEN DO;
CALL PRINT$NUMBER(N);
IF COL < 9 THEN DO;
COL = COL + 1;
CALL PRINT(.(9,36));
END;
ELSE DO;
COL = 0;
CALL PRINT(.(13,10,36));
END;
END;
END;
END;
END;
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre>2 3 5 7 11 13 17 19 23 29
37 47 59 67 79 89 113 127 137 139
149 157 167 179 199 223 227 229 233 239
257 269 277 337 347 349 359 367 379 389
449 457 467 479 499 557 569 577 599 677</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''Primes with monotonic (rising or equal) digits'''
 
from operator import le
Line 612 ⟶ 1,277:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
{{Out}}
<pre>50 matches for base 10:
Line 623 ⟶ 1,288:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my $range = ^1000;
 
for flat 2..10, 17, 27, 31 -> $base {
Line 629 ⟶ 1,294:
.batch(20)».fmt("%{.tail.chars}s").join: "\n" }"
given $range.grep( *.is-prime ).map( *.base: $base ).grep: { [le] .comb }
}</langsyntaxhighlight>
{{out}}
<pre>Base 2: 4 non-decending primes between 0 and 1111100111:
Line 689 ⟶ 1,354:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds & displays primes whose decimal digits are in non─decreasing order.*/
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 1000 /*Not specified? Then use the default.*/
Line 732 ⟶ 1,397:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 748 ⟶ 1,413:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">load "stdlib.ring"
? "working..."
Line 781 ⟶ 1,446:
s = string(x) l = len(s)
if l > y y = l ok
return substr(" ", 11 - y + l) + s</langsyntaxhighlight>
{{out}}
<pre>working...
Line 793 ⟶ 1,458:
Found 50 base 10 primes with digits in nondecreasing order
done...</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
base = 10
upto = 1000
 
res = Prime.each(upto).select do |pr|
pr.digits(base).each_cons(2).all?{|p1, p2| p1 >= p2}
end
 
puts "There are #{res.size} non-descending primes below #{upto}:"
puts res.join(", ")
</syntaxhighlight>
{{out}}
<pre>There are 50 non-descending primes below 1000:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 37, 47, 59, 67, 79, 89, 113, 127, 137, 139, 149, 157, 167, 179, 199, 223, 227, 229, 233, 239, 257, 269, 277, 337, 347, 349, 359, 367, 379, 389, 449, 457, 467, 479, 499, 557, 569, 577, 599, 677
</pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
 
const func boolean: isPrime (in integer: number) is func
result
var boolean: prime is FALSE;
local
var integer: upTo is 0;
var integer: testNum is 3;
begin
if number = 2 then
prime := TRUE;
elsif odd(number) and number > 2 then
upTo := sqrt(number);
while number rem testNum <> 0 and testNum <= upTo do
testNum +:= 2;
end while;
prime := testNum > upTo;
end if;
end func;
 
 
const func boolean: isNondecreasing (in var integer: number) is func
result
var boolean: nondecreasing is TRUE;
local
var integer: previousDigit is 10;
var integer: currentDigit is 0;
begin
while number <> 0 and nondecreasing do
currentDigit := number rem 10;
if currentDigit > previousDigit then
nondecreasing := FALSE;
end if;
number := number div 10;
previousDigit := currentDigit;
end while;
end func;
 
 
const proc: main is func
local
var integer: n is 0;
begin
write("2 ");
for n range 3 to 999 step 2 do
if isPrime(n) and isNondecreasing(n) then
write(n <& " ");
end if;
end for;
end func;</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 19 23 29 37 47 59 67 79 89 113 127 137 139 149 157 167 179 199 223 227 229 233 239 257 269 277 337 347 349 359 367 379 389 449 457 467 479 499 557 569 577 599 677
</pre>
 
=={{header|Sidef}}==
Simple solution:
<syntaxhighlight lang="ruby">say 1000.primes.grep { .digits.cons(2).all { .head >= .tail } }</syntaxhighlight>
 
Generate such primes from digits (asymptotically faster):
<syntaxhighlight lang="ruby">func primes_with_nondecreasing_digits(upto, base = 10) {
 
upto = prev_prime(upto+1)
 
var list = []
var digits = @(1..^base -> flip)
 
var end_digits = digits.grep { .is_coprime(base) }
list << digits.grep { .is_prime && !.is_coprime(base) }...
 
for k in (0 .. upto.ilog(base)) {
digits.combinations_with_repetition(k, {|*a|
var v = a.digits2num(base)
next if (v*base + end_digits.tail > upto)
end_digits.each {|d|
var n = (v*base + d)
next if ((n >= base) && (a[0] > d))
list << n if n.is_prime
}
})
}
 
list.sort
}
 
say primes_with_nondecreasing_digits(1000)</syntaxhighlight>
{{out}}
<pre>
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 37, 47, 59, 67, 79, 89, 113, 127, 137, 139, 149, 157, 167, 179, 199, 223, 227, 229, 233, 239, 257, 269, 277, 337, 347, 349, 359, 367, 379, 389, 449, 457, 467, 479, 499, 557, 569, 577, 599, 677]
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Linq
Imports System.Collections.Generic
Imports System.Console
Line 850 ⟶ 1,626:
Next
End Sub
End Module</langsyntaxhighlight>
{{out}}
Same as C#.
Line 856 ⟶ 1,632:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./seqfmt" for LstFmt
import "/fmt" for Fmt
 
var nonDescending = Fn.new { |p|
Line 878 ⟶ 1,652:
for (p in primes) if (nonDescending.call(p)) nonDesc.add(p)
System.print("Primes below 1,000 with digits in non-decreasing order:")
Fmt.tprint("$3d", nonDesc, 10)
for (chunk in Lst.chunks(nonDesc, 10)) Fmt.print("$3d", chunk)
System.print("\n%(nonDesc.count) such primes found.")</langsyntaxhighlight>
 
{{out}}
Line 891 ⟶ 1,665:
 
50 such primes found.
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true;
];
 
func Nondecreasing(N);
\Return 'true' if N has left-to-right nondecreasing digits
\Or return 'true' if N has right-to-left nonincreasing digits
int N, D, D0;
[N:= N/10;
D0:= rem(0);
while N do
[N:= N/10;
D:= rem(0);
if D > D0 then return false;
D0:= D;
];
return true;
];
 
int Count, N;
[Count:= 0;
for N:= 0 to 1000-1 do
if IsPrime(N) and Nondecreasing(N) then
[IntOut(0, N);
Count:= Count+1;
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
CrLf(0);
IntOut(0, Count);
Text(0, " primes found with nondecreasing digits below 1000.
");
]</syntaxhighlight>
 
{{out}}
<pre>
2 3 5 7 11 13 17 19 23 29
37 47 59 67 79 89 113 127 137 139
149 157 167 179 199 223 227 229 233 239
257 269 277 337 347 349 359 367 379 389
449 457 467 479 499 557 569 577 599 677
 
50 primes found with nondecreasing digits below 1000.
</pre>
9,476

edits