Primes with digits in nondecreasing order: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Frink)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(4 intermediate revisions by 4 users not shown)
Line 9:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 34:
print(‘#3’.format(n), end' I c % 10 == 0 {"\n"} E ‘ ’)
print()
print(‘Found ’c‘ primes with digits in nondecreasing order’)</langsyntaxhighlight>
 
{{out}}
Line 49:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC NonDecreasingDigits(INT x)
Line 81:
OD
PrintF("%E%EThere are %I primes",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Primes_with_digits_in_nondecreasing_order.png Screenshot from Atari 8-bit computer]
Line 93:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find primes where the digits are non-descending #
INT max number = 1000;
# sieve the primes to max number #
Line 129:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 143:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">(⊢(/⍨)(∧/2≤/10(⊥⍣¯1)⊢)¨)∘(⊢(/⍨)(2=0+.=⍳|⊢)¨)⍳1000</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
Line 151:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">primes: select 1..1000 => prime?
nondecreasing?: function [n][
ds: digits n
Line 166:
 
loop split.every: 10 select primes => nondecreasing? 'a ->
print map a => [pad to :string & 4]</langsyntaxhighlight>
 
{{out}}
Line 177:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRIMES_WITH_DIGITS_IN_NONDECREASING_ORDER.AWK
BEGIN {
Line 209:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 223:
=={{header|BASIC}}==
{{works with|QBasic}}
<langsyntaxhighlight lang="basic">10 DEFINT A-Z
20 DIM P(1000)
30 P(0)=-1: P(1)=-1
Line 232:
80 N=A*100+B*10+C
90 IF P(N)=0 THEN PRINT N,
100 NEXT C,B,A</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11
Line 247:
 
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
Line 279:
next i
print chr(10); c; " such primes found."
end</langsyntaxhighlight>
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 325:
Next i
PrintN(#CRLF$ + Str(c) + " such primes found."): Input()
CloseConsole()</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">sub isPrime(v)
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
Line 360:
next i
print chr$(10), c, " such primes found."
end</langsyntaxhighlight>
 
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr";
 
let sieve(prime, max) be
Line 396:
$)
freevec(prime)
$)</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 19 23 29
Line 406:
=={{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 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 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 527 ⟶ 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 587 ⟶ 682:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">echo (([:*./2<:/\10#.^:_1])"0#])@(i.&.(p:^:_1)) 1000
exit 0</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|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
function is_ndp( byval n as integer ) as boolean
Line 608 ⟶ 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}}==
<langsyntaxhighlight lang="frink">for n = primes[2,1000]
if sort[integerDigits[n]] == integerDigits[n]
print["$n "]</langsyntaxhighlight>
{{out}}
<pre>
Line 623 ⟶ 718:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 660 ⟶ 755:
}
fmt.Printf("\n%d such primes found.\n", len(nonDesc))
}</langsyntaxhighlight>
 
{{out}}
Line 679 ⟶ 774:
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
<langsyntaxhighlight lang="jq">def digits: tostring | explode;
 
# Input: an upper bound, or `infinite`
Line 687 ⟶ 782:
| select( (digits | (. == sort)) and is_prime);
 
1000 | primes_with_nondecreasing_digits</langsyntaxhighlight>
{{out}} (abbreviated)
<pre>
Line 706 ⟶ 801:
{{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 716 ⟶ 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 792 ⟶ 887:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 841 ⟶ 936:
done
printf "\n%d Primes with digits in nondecreasing order < %d\n" ${cnt} $MAXPRIME
</syntaxhighlight>
</lang>
{{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
Line 847 ⟶ 942:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
BOOLEAN PRIME
DIMENSION PRIME(1000), COL(10)
Line 877 ⟶ 972:
TEST CONTINUE
VECTOR VALUES CFMT = $10(I4)*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 19 23 29
Line 885 ⟶ 980:
449 457 467 479 499 557 569 577 599 677</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Partition[
Cases[Most@
NestWhileList[NextPrime,
2, # < 1000 &], _?(OrderedQ[IntegerDigits@#] &)],
UpTo[10]] // TableForm</langsyntaxhighlight>
 
{{out}}<pre>
Line 900 ⟶ 995:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat, sugar
 
func isPrime(n: Natural): bool =
Line 930 ⟶ 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 941 ⟶ 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 953 ⟶ 1,048:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 960 ⟶ 1,055:
 
my @want = grep ! /(.)(.)(??{$1 > $2 ? '' : '(*FAIL)'})/, @{ primes(1000) };
print "@want" =~ s/.{50}\K /\n/gr . "\n\ncount: " . @want . "\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 972 ⟶ 1,067:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Loop.
Line 995 ⟶ 1,090:
If the number is not prime, say no.
If the number is not non-decreasing, say no.
Say yes.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,002 ⟶ 1,097:
 
=={{header|PL/M}}==
<langsyntaxhighlight 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;
Line 1,058 ⟶ 1,153:
END;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre>2 3 5 7 11 13 17 19 23 29
Line 1,067 ⟶ 1,162:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''Primes with monotonic (rising or equal) digits'''
 
from operator import le
Line 1,182 ⟶ 1,277:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
{{Out}}
<pre>50 matches for base 10:
Line 1,193 ⟶ 1,288:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my $range = ^1000;
 
for flat 2..10, 17, 27, 31 -> $base {
Line 1,199 ⟶ 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 1,259 ⟶ 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 1,302 ⟶ 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 1,318 ⟶ 1,413:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">load "stdlib.ring"
? "working..."
Line 1,351 ⟶ 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 1,363 ⟶ 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}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
 
Line 1,415 ⟶ 1,528:
end if;
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,423 ⟶ 1,536:
=={{header|Sidef}}==
Simple solution:
<langsyntaxhighlight lang="ruby">say 1000.primes.grep { .digits.cons(2).all { .head >= .tail } }</langsyntaxhighlight>
 
Generate such primes from digits (asymptotically faster):
<langsyntaxhighlight lang="ruby">func primes_with_nondecreasing_digits(upto, base = 10) {
 
upto = prev_prime(upto+1)
Line 1,451 ⟶ 1,564:
}
 
say primes_with_nondecreasing_digits(1000)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,459 ⟶ 1,572:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Linq
Imports System.Collections.Generic
Imports System.Console
Line 1,513 ⟶ 1,626:
Next
End Sub
End Module</langsyntaxhighlight>
{{out}}
Same as C#.
Line 1,519 ⟶ 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 1,541 ⟶ 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 1,557 ⟶ 1,668:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 1,592 ⟶ 1,703:
Text(0, " primes found with nondecreasing digits below 1000.
");
]</langsyntaxhighlight>
 
{{out}}
9,476

edits