Ascending primes: Difference between revisions

m
no edit summary
mNo edit summary
 
(104 intermediate revisions by 20 users not shown)
Line 16:
*[[Primes with digits in nondecreasing order]] (infinite series allowing duplicate digits, whereas this isn't and doesn't)
*[[Pandigital prime]] (whereas this is the smallest, with gaps in the used digits being permitted)
*[[Descending primes]]
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F isprime(n)
I n == 2
R 1B
I n == 1 | n % 2 == 0
R 0B
V root1 = Int(n ^ 0.5) + 1
L(k) (3 .< root1).step(2)
I n % k == 0
R 0B
R 1B
 
V queue = Array(1..9)
[Int] primes
 
L !queue.empty
V n = queue.pop(0)
I isprime(n)
primes.append(n)
queue.extend((n % 10 + 1 .< 10).map(k -> @n * 10 + k))
 
print(primes)</syntaxhighlight>
 
{{out}}
<pre>
[2, 3, 5, 7, 13, 17, 19, 23, 29, 37, 47, 59, 67, 79, 89, 127, 137, 139, 149, 157, 167, 179, 239, 257, 269, 347, 349, 359, 367, 379, 389, 457, 467, 479, 569, 1237, 1249, 1259, 1279, 1289, 1367, 1459, 1489, 1567, 1579, 1789, 2347, 2357, 2389, 2459, 2467, 2579, 2689, 2789, 3457, 3467, 3469, 4567, 4679, 4789, 5689, 12347, 12379, 12457, 12479, 12569, 12589, 12689, 13457, 13469, 13567, 13679, 13789, 15679, 23459, 23567, 23689, 23789, 25679, 34589, 34679, 123457, 123479, 124567, 124679, 125789, 134789, 145679, 234589, 235679, 235789, 245789, 345679, 345689, 1234789, 1235789, 1245689, 1456789, 12356789, 23456789]
</pre>
 
=={{header|ALGOL 68}}==
Line 25 ⟶ 56:
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-rows}}
<langsyntaxhighlight lang="algol68">BEGIN # find all primes with strictly increasing digits #
PR read "primes.incl.a68" PR # include prime utilities #
PR read "rows.incl.a68" PR # include array utilities #
Line 68 ⟶ 99:
IF i MOD 10 = 0 THEN print( ( newline ) ) FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 82 ⟶ 113:
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
</pre>
 
=={{header|ALGOL W}}==
{{Trans|Lua}}
...and only a few characters different from the Algol W [[Descending primes]] sample.
<syntaxhighlight lang="algolw">
begin % find all primes with strictly ascending digits - translation of Lua %
 
% quicksorts v, the bounds of v must be specified in lb and ub %
procedure quicksort ( integer array v( * )
; integer value lb, ub
) ;
if ub > lb then begin
% more than one element, so must sort %
integer left, right, pivot;
left := lb;
right := ub;
% choosing the middle element of the array as the pivot %
pivot := v( left + ( ( right + 1 ) - left ) div 2 );
while begin
while left <= ub and v( left ) < pivot do left := left + 1;
while right >= lb and v( right ) > pivot do right := right - 1;
left <= right
end do begin
integer swap;
swap := v( left );
v( left ) := v( right );
v( right ) := swap;
left := left + 1;
right := right - 1
end while_left_le_right ;
quicksort( v, lb, right );
quicksort( v, left, ub )
end quicksort ;
 
% returns true if n is prime, false otherwise %
logical procedure is_prime( integer value n ) ;
if n < 2 then false
else if n rem 2 = 0 then n = 2
else if n rem 3 = 0 then n = 3
else begin
logical prime; prime := true;
for f := 5 step 6 until entier( sqrt( n ) ) do begin
if n rem f = 0 or n rem ( f + 2 ) = 0 then begin
prime := false;
goto done
end if_n_rem_f_eq_0_or_n_rem_f_plus_2_eq_0
end for_f;
done: prime
end is_prime ;
 
% increments n and also returns its new value %
integer procedure inc ( integer value result n ) ; begin n := n + 1; n end;
 
% sets primes to the list of ascending primes and lenPrimes to the %
% number of ascending primes - primes must be big enough, e.g. have 511 %
% elements %
procedure ascending_primes ( integer array primes ( * )
; integer result lenPrimes
) ;
begin
integer array digits ( 1 :: 9 );
integer array candidates ( 1 :: 6000 );
integer lenCandidates;
candidates( 1 ) := 0;
lenCandidates := 1;
lenPrimes := 0;
for i := 1 until 9 do digits( i ) := i;
for i := 1 until 9 do begin
for j := 1 until lenCandidates do begin
integer cValue; cValue := candidates( j ) * 10 + digits( i );
if is_prime( cValue ) then primes( inc( lenPrimes ) ) := cValue;
candidates( inc( lenCandidates ) ) := cValue
end for_j
end for_i ;
quickSort( primes, 1, lenPrimes );
end ascending_primes ;
 
begin % find the ascending primes and print them %
integer array primes ( 1 :: 512 );
integer lenPrimes;
ascending_primes( primes, lenPrimes );
for i := 1 until lenPrimes do begin
writeon( i_w := 8, s_w := 0, " ", primes( i ) );
if i rem 10 = 0 then write()
end for_i
end
end.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 13 17 19 23 29 37
47 59 67 79 89 127 137 139 149 157
167 179 239 257 269 347 349 359 367 379
389 457 467 479 569 1237 1249 1259 1279 1289
1367 1459 1489 1567 1579 1789 2347 2357 2389 2459
2467 2579 2689 2789 3457 3467 3469 4567 4679 4789
5689 12347 12379 12457 12479 12569 12589 12689 13457 13469
13567 13679 13789 15679 23459 23567 23689 23789 25679 34589
34679 123457 123479 124567 124679 125789 134789 145679 234589 235679
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
</pre>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">ascending?: function [x][
initial: digits x
and? [equal? sort initial initial][equal? size initial size unique initial]
Line 98 ⟶ 231:
loop split.every:10 ascendingNums 'nums [
print map nums 'num -> pad to :string num 10
]</langsyntaxhighlight>
 
{{out}}
Line 114 ⟶ 247:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ASCENDING_PRIMES.AWK
BEGIN {
Line 151 ⟶ 284:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 169 ⟶ 302:
 
=={{header|C}}==
{{trans|Fortran}}
<lang C>/*
<syntaxhighlight lang="c">/*
* The program prints all prime numbers whose digits in the decimal notation
* Ascending primes
* are arranged in a strictly ascending sequence.
*
* Generate and show all primes with strictly ascending decimal digits.
* Note that the largest such number must be less than 123456789.
*
*
* Solution
*
* We only consider positive numbers in the range 1 to 123456789. We would
* get 7027260 primes, because there are so many primes smaller than 123456789
* (see also Wolfram Alpha).On the other hand, there are only 511 distinct
* nonzero positive integers having their digits arranged in ascending order.
* Therefore, it is better to start with numbers that have properly arranged
* digitsand then check if they are prime numbers.The method of generating
* a sequence of such numbers is not indifferent.We want this sequence to be
* monotonically increasing, because then additional sorting of results will
* be unnecessary. It turns out that by using a queue we can easily get the
* desired effect. Additionally, the algorithm then does not use recursion
* (although the program probably does not have to comply with the MISRA
* standard). The problem to be solved is the queue size, the a priori
* assumption that 1000 is good enough, but a bit magical.
*/
 
Line 181 ⟶ 331:
#include <math.h>
 
#if UINT_MAX < 123456789
// We don't know how big an array is needed to store all the results.
#error "we need at least 9 decimal digits (32-bit integers)"
// We could use a dynamic array that grows in size with realloc() calls.
#endif
// But it is easier to just declare a fixed size and check if it was big
 
// enough or if it should be enlarged and then run the program again.
 
//
#define MAXSIZE 1000
 
unsigned primesCounter = 0queue[MAXSIZE];
unsigned primes[MAXSIZE];
 
unsigned begin = 0;
/*
unsigned end = 0;
* A brute force method of checking if a number is prime.
unsigned n = 0;
*/
 
bool isPrime(unsigned value)
 
bool isPrime(unsigned n)
{
if (n == 2)
// We don't want to contemplate whether the rest of
{
// the algorithm can handle special cases or not.
return true;
//
}
if (value == 0 || value == 1)
if (n == 1 || n % 2 == 0)
{
return false;
}
 
unsigned truncatedSquareRootroot = sqrt(valuen);
for (unsigned k = 23; k <= truncatedSquareRootroot; k ++= 2)
{
if (value % k == 0)
if (n % k == 0)
{
return false;
}
 
}
return true;
}
 
/*
* We check if we didn't hit the prime number. Then we add the next
* digit to the sequence so that the sequence is increasing and we
* call it recursively for the next digits.
*/
void recursive(unsigned m, unsigned value)
{
// Space exhausted, no fun, no output.
//
if (primesCounter == MAXSIZE)
exit(EXIT_FAILURE);
 
if (isPrime(value))
primes[primesCounter++] = value;
 
for (int k = m + 1; k <= 9; k++)
recursive(k, 10 * value + k);
}
 
int cmp(const unsigned* a, const unsigned* b)
{
return *a - *b;
}
 
int main(int argc, char argv[])
{
for (int k = 1; k <= 9; k++)
{
recursive(k, k);
queue[end++] = k;
}
 
while (begin < end)
qsort(primes, primesCounter, sizeof(unsigned long), cmp);
{
int value = queue[begin++];
if (isPrime(value))
{
primes[n++] = value;
}
for (int k = value % 10 + 1; k <= 9; k++)
{
queue[end++] = value * 10 + k;
}
}
 
for (int k = 0; k < primesCountern; k++)
{
printf("%9lu%c", primes[k], k % 8 == 7 ? '\n' : ' ');
printf("%u ", primes[k]);
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
{{output}}
<pre>2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789 </pre>
<pre> 2 3 5 7 13 17 19 23
 
29 37 47 59 67 79 89 127
137 139 149 157 167 179 239 257
269 347 349 359 367 379 389 457
467 479 569 1237 1249 1259 1279 1289
1367 1459 1489 1567 1579 1789 2347 2357
2389 2459 2467 2579 2689 2789 3457 3467
3469 4567 4679 4789 5689 12347 12379 12457
12479 12569 12589 12689 13457 13469 13567 13679
13789 15679 23459 23567 23689 23789 25679 34589
34679 123457 123479 124567 124679 125789 134789 145679
234589 235679 235789 245789 345679 345689 1234789 1235789
1245689 1456789 12356789 23456789
</pre>
=={{header|C++}}==
{{trans|C}}
<lang Cpp>#include <algorithm>
<syntaxhighlight lang="cpp">/*
* Ascending primes
*
* Generate and show all primes with strictly ascending decimal digits.
*
*
* Solution
*
* We only consider positive numbers in the range 1 to 123456789. We would
* get 7027260 primes, because there are so many primes smaller than 123456789
* (see also Wolfram Alpha).On the other hand, there are only 511 distinct
* nonzero positive integers having their digits arranged in ascending order.
* Therefore, it is better to start with numbers that have properly arranged
* digitsand then check if they are prime numbers.The method of generating
* a sequence of such numbers is not indifferent.We want this sequence to be
* monotonically increasing, because then additional sorting of results will
* be unnecessary. It turns out that by using a queue we can easily get the
* desired effect. Additionally, the algorithm then does not use recursion
* (although the program probably does not have to comply with the MISRA
* standard). The problem to be solved is the queue size, the a priori
* assumption that 1000 is good enough, but a bit magical.
*/
 
#include <cmath>
#include <iostream>
#include <stackqueue>
#include <vector>
 
Line 271 ⟶ 431:
 
 
queue<unsigned> suspected;
struct Item
{
unsigned lastDigit;
unsigned currentValue;
};
 
 
stack<Item> suspected;
vector<unsigned> primes;
 
 
bool isPrime(unsigned valuen)
{
if (valuen == 0 || value == 12)
{
return true;
}
if (n == 1 || n % 2 == 0)
{
return false;
}
elseunsigned ifroot (value != 2sqrt(n);
for (unsigned k = 3; k <= root; k += 2)
{
if (valuen % 2k == 0)
{
return false;
}
else
{
unsigned truncatedSquareRoot = sqrt(value);
for (unsigned k = 3; k <= truncatedSquareRoot; k += 2)
if (value % k == 0)
return false;
}
}
Line 310 ⟶ 461:
for (unsigned k = 1; k <= 9; k++)
{
suspected.push(Item{ k, k });
}
 
while (!suspected.empty())
{
Itemint itemn = suspected.topfront();
suspected.pop();
 
if (isPrime(item.currentValuen))
{
primes.push_back(item.currentValuen);
}
 
// The value of n % 10 gives the least significient digit of n
for (unsigned k = item.lastDigit + 1; k <= 9; k++)
//
for (unsigned k = n % 10 + 1; k <= 9; k++)
{
suspected.push(Item{ k, item.currentValuen * 10 + k });
}
}
 
sort(primes.begin(), primes.end());
copy(primes.begin(), primes.end(), ostream_iterator<unsigned>(cout, " "));
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
{{Output}}
<pre>2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789 </pre>
 
=={{header|C#}}==
{{trans|PHP}}
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
namespace ascendingprimes
{
class Program
{
static bool isPrime(uint n)
{
if (n == 2)
return true;
if (n == 1 || n % 2 = 0)
return false;
uint root = (uint)Math.Sqrt(n);
for (uint k = 3; k <= root; k += 2)
if (n % k == 0)
return false;
return true;
}
static void Main(string[] args)
{
var queue = new Queue<uint>();
var primes = new List<uint>();
 
for (uint k = 1; k <= 9; k++)
queue.Enqueue(k);
while(queue.Count > 0)
{
uint n = queue.Dequeue();
if (isPrime(n))
primes.Add(n);
for (uint k = n % 10 + 1; k <= 9; k++)
queue.Enqueue(n * 10 + k);
}
 
foreach (uint p in primes)
{
Console.Write(p);
Console.Write(" ");
}
Console.WriteLine();
}
}
}</syntaxhighlight>
{{Output}}
<pre>2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Windows,SysUtils,StdCtrls}}
 
<syntaxhighlight lang="Delphi">
uses Windows,SysUtils,StdCtrls;
 
type TProgress = procedure(Percent: integer);
 
procedure ShowAscendingPrimes(Memo: TMemo; Prog: TProgress);
 
implementation
 
 
function IsPrime(N: integer): boolean;
{Optimised prime test - about 40% faster than the naive approach}
var I,Stop: integer;
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));
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;
 
 
 
function IsAscending(N: integer): boolean;
{Determine if each digit is greater than previous, left to right}
var S: string;
var I: integer;
begin
Result:=False;
S:=IntToStr(N);
for I:=1 to Length(S)-1 do
if S[I]>=S[I+1] then exit;
Result:=True;
end;
 
 
procedure ShowAscendingPrimes(Memo: TMemo; Prog: TProgress);
{Write Ascending primes up to 123,456,789 }
{It has an optional, user-supplied progress routine }
var I,Cnt: integer;
var S: string;
const Max = 123456789;
begin
if Assigned(Prog) then Prog(0);
S:='';
Cnt:=0;
for I:=2 to Max do
begin
if ((I mod 1000000)=0) and Assigned(Prog) then Prog(Trunc(100*(I/Max)));
if IsAscending(I) and IsPrime(I) then
begin
S:=S+Format('%9.0d', [I]);
Inc(Cnt);
if (Cnt mod 10)=0 then
begin
Memo.Lines.Add(S);
S:='';
end;
end;
end;
end;
 
</syntaxhighlight>
 
{{out}}
 
<pre>
2 3 5 7 13 17 19 23 29 37
47 59 67 79 89 127 137 139 149 157
167 179 239 257 269 347 349 359 367 379
389 457 467 479 569 1237 1249 1259 1279 1289
1367 1459 1489 1567 1579 1789 2347 2357 2389 2459
2467 2579 2689 2789 3457 3467 3469 4567 4679 4789
5689 12347 12379 12457 12479 12569 12589 12689 13457 13469
13567 13679 13789 15679 23459 23567 23689 23789 25679 34589
34679 123457 123479 124567 124679 125789 134789 145679 234589 235679
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
</pre>
 
=={{header|Dylan}}==
{{trans|C++}}
 
[https://play.opendylan.org/shared/d04d21b97a8659b7 View in Dylan playground]
 
<syntaxhighlight lang="dylan">
define function prime? (n :: <integer>) => (p :: <boolean>)
case
n == 2
=> #t;
n == 1 | remainder(n, 2) == 0
=> #f;
otherwise
=> let root = sqrt(as(<double-float>, n));
iterate loop (k = 3)
case
remainder(n, k) == 0 => #f;
k > root => #t;
otherwise => loop(k + 2);
end
end
end case
end function;
 
define function ascending-primes () => (primes :: <sequence>)
let maybe = make(<deque>);
for (k from 1 to 9)
push-last(maybe, k)
end;
let primes = make(<stretchy-vector>);
while (~empty?(maybe))
let n = pop(maybe);
if (prime?(n))
add!(primes, n)
end;
for (k from modulo(n, 10) + 1 to 9)
push-last(maybe, n * 10 + k)
end
end;
primes
end function;
 
format-out("%=", ascending-primes());
</syntaxhighlight>
 
{{out}}
<pre>{stretchy vector 2, 5, 7, 13, 17, 19, 23, 29, 37, 47, 59, 67, 79, 89, 127, 137, 139, 149, 157, 167, 179, 239, 257, 269, 347, 349, 359, 367, 379, 389, 457, 467, 479, 569, 1237, 1249, 1259, 1279, 1289, 1367, 1459, 1489, 1567, 1579, 1789, 2347, 2357, 2389, 2459, 2467, 2579, 2689, 2789, 3457, 3467, 3469, 4567, 4679, 4789, 5689, 12347, 12379, 12457, 12479, 12569, 12589, 12689, 13457, 13469, 13567, 13679, 13789, 15679, 23459, 23567, 23689, 23789, 25679, 34589, 34679, 123457, 123479, 124567, 124679, 125789, 134789, 145679, 234589, 235679, 235789, 245789, 345679, 345689, 1234789, 1235789, 1245689, 1456789, 12356789, 23456789}</pre>
 
=={{header|EasyLang}}==
 
This outputs all 100 ascending primes. They are not sorted - that was not demanded anyway.
 
<syntaxhighlight lang=easylang>
func isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
proc nextasc n . .
if isprim n = 1
write n & " "
.
if n > 123456789
return
.
for d = n mod 10 + 1 to 9
nextasc n * 10 + d
.
.
nextasc 0
</syntaxhighlight>
 
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Ascending primes. Nigel Galloway: April 19th., 2022
[2;3;5;7]::List.unfold(fun(n,i)->match n with []->None |_->let n=n|>List.map(fun(n,g)->[for n in n.. -1..1->(n-1,i*n+g)])|>List.concat in Some(n|>List.choose(fun(_,n)->if isPrime n then Some n else None),(n|>List.filter(fst>>(<)0),i*10)))([(2,3);(6,7);(8,9)],10)
|>List.concat|>List.sort|>List.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
</pre>
 
=={{header|Factor}}==
The approach taken is to check the members of the powerset of [1..9] (of which there are only 512 if you include the empty set) for primality.
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: grouping math math.combinatorics math.functions
math.primes math.ranges prettyprint sequences sequences.extras ;
 
9 [1,b] all-subsets [ reverse 0 [ 10^ * + ] reduce-index ]
[ prime? ] map-filter 10 group simple-table.</langsyntaxhighlight>
{{out}}
<pre>
Line 369 ⟶ 743:
34679 123457 123479 124567 124679 125789 134789 145679 234589 235679
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
</pre>
 
 
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
<br>
<syntaxhighlight lang="forth">#! /usr/bin/gforth
 
\ Ascending primes
 
\ checks (by simple trial-division) whether the TOS is prime
: prime? ( n -- f )
dup 1 <= IF
drop false
ELSE
dup 2 = IF
drop true
ELSE
2
BEGIN
2dup dup * > >r
2dup mod 0> r>
over and
WHILE
drop 1+
REPEAT
nip nip
THEN
THEN
;
 
: ascending-primes-aux ( n i -- )
dup 10 = IF
drop
dup prime? IF
.
ELSE
drop
THEN
ELSE
2dup 1+ recurse \ do not include digit i
swap 10 * over + swap 1+ recurse \ do include digit i
THEN
;
 
\ prints all primes with strictly ascending digits
: ascending-primes ( -- )
0 1 ascending-primes-aux cr
;
 
ascending-primes bye
</syntaxhighlight>
 
{{out}}
<pre>
./ascending-primes.fs
89 7 79 67 5 59 569 5689 47 479 4789 467 4679 457 4567 3 389 37 379 367 359 349 347 3469 3467 34679 34589 3457 345689 345679 2 29 2789 269 2689 257 2579 25679 2467 2459 245789 23 239 2389 23789 23689 2357 235789 23567 235679 2347 23459 234589 23456789 19 17 179 1789 167 157 1579 1567 15679 149 1489 1459 145679 1456789 13 139 137 13789 1367 13679 13567 134789 13469 13457 1289 127 1279 12689 1259 12589 125789 12569 1249 12479 124679 12457 1245689 124567 1237 12379 1235789 12356789 12347 123479 1234789 123457
</pre>
 
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">! Ascending primes
Modern Fortran allows recursive calls, but the code below uses the stack to avoid recursion. Insertion sort is also used.
!
<lang Fortran>logical function isprime(n)
! Generate and show all primes with strictly ascending decimal digits.
!
!
! Solution
!
! We only consider positive numbers in the range 1 to 123456789. We would get
! 7027260 primes, because there are so many primes smaller than 123456789 (see
! also Wolfram Alpha). On the other hand, there are only 511 distinct positive
! integers having their digits arranged in ascending order. Therefore, it is
! better to start with numbers that have properly arranged digits and then check
! if they are prime numbers. The method of generating a sequence of such numbers
! is not indifferent. We want this sequence to be monotonically increasing,
! because then additional sorting of results will be unnecessary. It turns out
! that by using a queue we can easily get the desired effect. Additionally, the
! algorithm then does not use recursion (although the program probably does not
! have to comply with the MISRA standard). The problem to be solved is the queue
! size, the a priori assumption that 1000 is good enough, but a bit magical.
 
program prog
 
parameter (MAXSIZE = 1000)
logical isprime
dimension iqueue(MAXSIZE)
dimension iprimes(MAXSIZE)
ibegin = 1
iend = 1
n = 0
 
do k = 1, 9
iqueue(iend) = k
iend = iend + 1
end do
do while (ibegin .lt. iend)
iv = iqueue(ibegin)
ibegin = ibegin + 1
if (isprime(iv)) then
n = n + 1
iprimes(n) = iv
end if
lsd1 = mod(iv, 10) + 1
if (lsd1 .le. 9) then
do k = lsd1, 9
iqueue(iend) = iv * 10 + k
iend = iend + 1
end do
end if
end do
 
print *, (iprimes(i), i = 1, n)
 
end program
 
 
logical function isprime(n)
! Slightly improved algorithm for checking if a number is prime.
Line 381 ⟶ 870:
! the square root of that number.
!
! Positive numbers only. BTW, negative numbers are prime numbers if their absolute values
! if their absolute values are prime numbers.
 
isprime = .FALSE.
if (n .lt. 0) then
n = -n
endif
if (n .eq. 0 .or. n .eq. 1) then
return
Line 403 ⟶ 889:
end if
isprime = .TRUE.
end function</syntaxhighlight>
 
 
program prog
 
parameter (MAXSIZE = 1000)
dimension iprimes(MAXSIZE), lastdigit(MAXSIZE), ivalue(MAXSIZE)
logical isprime
 
n = 0
isp = 0
 
do k = 1, 9
isp = isp + 1
if (isp .ge. MAXSIZE) then
stop
end if
lastdigit(isp) = k
ivalue(isp) = k
end do
 
do while (isp .gt. 0)
 
ld = lastdigit(isp)
iv = ivalue(isp)
isp = isp - 1
 
if (isprime(iv)) then
if (n .ge. MAXSIZE) then
stop
end if
j = 1
do while (j .le. n .and. iprimes(j) .le. iv)
j = j + 1
end do
do k = n, j, -1
iprimes(k + 1) = iprimes(k)
end do
iprimes(j) = iv
n = n + 1
end if
ld1 = ld + 1
do k = ld1, 9
isp = isp + 1
if (isp .ge. MAXSIZE) then
stop
end if
lastdigit(isp) = k
ivalue(isp) = iv * 10 + k
end do
end do
 
print *, (iprimes(i), i = 1, n)
 
end program
</lang>
{{Output}}
The estimated execution time is 1.5 milliseconds on the same hardware on which the Java program was run. It should be remembered that modern CPUs do not have a constant clock speed and additionally the measured times depend on the system load with other tasks. Nevertheless, the Fortran program seems to be 4 times faster than the Java program.
Line 479 ⟶ 909:
235789 245789 345679 345689 1234789 1235789
1245689 1456789 12356789 23456789
</pre>
 
=={{header|FreeBASIC}}==
===Power Set===
{{trans|XPL0}}
<syntaxhighlight lang="vb">#include "isprime.bas"
#include "sort.bas"
 
Dim As Integer i, n, tmp, num, cant = 0
Dim Shared As Integer matriz(512)
For i = 0 To Ubound(matriz)-1
n = 0
tmp = i
num = 1
While tmp
If tmp And 1 Then n = n * 10 + num
tmp Shr= 1
num += 1
Wend
matriz(i)= n
Next i
 
Sort(matriz())
 
For i = 1 To Ubound(matriz)-1 'skip empty set
n = matriz(i)
If isPrime(n) Then
Print Using "#########"; n;
cant += 1
If cant Mod 10 = 0 Then Print
End If
Next i
Print Using !"\nThere are & ascending primes."; cant
 
Sleep</syntaxhighlight>
 
 
{{out}}
<pre> 2 3 5 7 13 17 19 23 29 37
47 59 67 79 89 127 137 139 149 157
167 179 239 257 269 347 349 359 367 379
389 457 467 479 569 1237 1249 1259 1279 1289
1367 1459 1489 1567 1579 1789 2347 2357 2389 2459
2467 2579 2689 2789 3457 3467 3469 4567 4679 4789
5689 12347 12379 12457 12479 12569 12589 12689 13457 13469
13567 13679 13789 15679 23459 23567 23689 23789 25679 34589
34679 123457 123479 124567 124679 125789 134789 145679 234589 235679
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
 
There are 100 ascending primes.</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
void local fn AscendingPrimes( limit as long )
long i, n, mask, num, count = 0
for i = 0 to limit -1
n = 0 : mask = i : num = 1
while ( mask )
if mask & 1 then n = n * 10 + num
mask = mask >> 1
num++
wend
mda(i) = n
next
mda_sort @"compare:"
for i = 1 to mda_count (0) - 1
n = mda_integer(i)
if ( fn IsPrime( n ) )
printf @"%10ld\b", n
count++
if count mod 10 == 0 then print
end if
next
printf @"\n\tThere are %ld ascending primes.", count
end fn
 
window 1, @"Ascending Primes", ( 0, 0, 780, 230 )
print
 
CFTimeInterval t
t = fn CACurrentMediaTime
fn AscendingPrimes( 512 )
printf @"\n\tCompute time: %.3f ms\n",(fn CACurrentMediaTime-t)*1000
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
2 3 5 7 13 17 19 23 29 37
47 59 67 79 89 127 137 139 149 157
167 179 239 257 269 347 349 359 367 379
389 457 467 479 569 1237 1249 1259 1279 1289
1367 1459 1489 1567 1579 1789 2347 2357 2389 2459
2467 2579 2689 2789 3457 3467 3469 4567 4679 4789
5689 12347 12379 12457 12479 12569 12589 12689 13457 13469
13567 13679 13789 15679 23459 23567 23689 23789 25679 34589
34679 123457 123479 124567 124679 125789 134789 145679 234589 235679
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
 
There are 100 ascending primes.
 
Compute time: 9.008 ms
</pre>
 
Line 485 ⟶ 1,032:
{{libheader|Go-rcu}}
Using a generator.
<langsyntaxhighlight lang="go">package main
 
import (
Line 527 ⟶ 1,074:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 545 ⟶ 1,092:
 
=={{header|J}}==
Compare with [[Descending_primes#J|Descending primes]].
 
[https://jsoftware.github.io/j-playground/bin/html/#code=_10%20%5D%5C%20%2F%3A~%20(%23~%201%26p%3A)%2010%26%23.%40I.%20%23%3A%20i.%20513 Live link].
Compare with [[Descending primes#J|Descending primes]].
<syntaxhighlight lang="j">_10 ]\ /:~ (#~ 1&p:) 10&#.@I. #: i. 513</syntaxhighlight>
 
{{out}}
[https://jsoftware.github.io/j-playground/bin/html/emj.html#code=%20%20%20extend%3D%3A%20%7B%7B%20y%3B(1%2Beach%20i._1%2B%7B.y)%2CL%3A0%20y%20%7D%7D%0D%0A%20%20%2010%2010%24(%23~%201%20p%3A%20%5D)10%23.%26%3E(%5B%3A~.%40%3Bextend%20each)%5E%3A%23%20%3E%3Ai.9 Live link].
<pre>
 
2 3 5 7 13 17 19 23 29 37
<lang J> extend=: {{ y;(1+each i._1+{.y),L:0 y }}
47 59 67 79 89 127 137 139 149 157
$(#~ 1 p: ])10#.&>([:~.@;extend each)^:# >:i.9
167 179 239 257 269 347 349 359 367 379
100
389 457 467 479 569 1237 1249 1259 1279 1289
10 10$(#~ 1 p: ])10#.&>([:~.@;extend each)^:# >:i.9
1367 21459 1489 31567 1579 13 1789 23 2347 2357 5 2389 7 17 37 47 672459
2467 127 2579 137 2689 3472789 1573457 3467 257 3469 457 4567 167 4679 367 467 12374789
23475689 12347 2357 12379 345712457 136712479 12569 2467 12589 3467 12689 1567 13457 4567 12347 1245713469
13457 13567 2356713679 123457 13789 124567 15679 23459 19 23567 29 59 23689 23789 79 25679 8934589
34679 123457 123479 124567 124679 125789 134789 145679 234589 235679
139 239 149 349 359 269 569 179 379 479
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
389 1249 1259 1459 2459 3469 1279 1579 2579 4679
</pre>
1289 2389 1489 2689 5689 1789 2789 4789 23459 13469
12569 12379 12479 13679 34679 15679 25679 12589 34589 12689
23689 13789 23789 123479 124679 235679 145679 345679 234589 345689
134789 125789 235789 245789 1245689 1234789 1235789 1456789 12356789 23456789
timex'(#~ 1 p: ])10#.&>([:~.@;extend each)^:# >:i.9' NB. seconds (take with grain of salt)
0.003818
</lang>
 
cpu here was a 1.2ghz i3-1005g1
 
=={{header|Java}}==
{{trans|C++}}
<lang Java>package example.rossetacode.ascendingprimes;
<syntaxhighlight lang="java">/*
* Ascending primes
*
* Generate and show all primes with strictly ascending decimal digits.
*
*
* Solution
*
* We only consider positive numbers in the range 1 to 123456789. We would
* get 7027260 primes, because there are so many primes smaller than 123456789
* (see also Wolfram Alpha).On the other hand, there are only 511 distinct
* positive integers having their digits arranged in ascending order.
* Therefore, it is better to start with numbers that have properly arranged
* digits and then check if they are prime numbers.The method of generating
* a sequence of such numbers is not indifferent.We want this sequence to be
* monotonically increasing, because then additional sorting of results will
* be unnecessary. It turns out that by using a queue we can easily get the
* desired effect. Additionally, the algorithm then does not use recursion
* (although the program probably does not have to comply with the MISRA
* standard). The problem to be solved is the queue size, the a priori
* assumption that 1000 is good enough, but a bit magical.
*/
 
package example.rossetacode.ascendingprimes;
import java.util.ArrayList;
import java.util.List;
 
import java.util.Arrays;
public class Program implements Runnable {
 
public class Program implements Runnable {
private final int initialSize = 1000;
private final List<Integer> values = new ArrayList<>(initialSize);
 
public static void main(String[] args) {
Line 591 ⟶ 1,151:
public void run() {
 
final int MAX_SIZE = 1000;
// Generating numbers whose digits are arranged in a strictly ascending
final int[] queue = new int[MAX_SIZE];
// sequence.
//int begin = 0;
int end = 0;
 
for (int k = 1; k <= 9; k++) {
recursive(k,queue[end++] = k);
}
 
while (begin < end) {
// Filter out non-prime numbers, sort, and list the results. To perform
int n = queue[begin++];
// parallel calculations on multiple cores, simply replace stream() with
for (int k = n % 10 + 1; k <= 9; k++) {
// parallelStream(). It is surprising, however, that parallel processing
queue[end++] = n * 10 + k;
// does not shorten, but even slightly increases the computation time.
}
}
 
// We can use a parallel stream (and then sort the results)
// to use multiple cores.
//
System.out.println(valuesArrays.stream(queue).filter(this::isPrime).sortedboxed().toList());
}
 
private boolean isPrime(int valuen) {
if (valuen == 02) || value == 1){
return true;
}
if (n == 1 || n % 2 == 0) {
return false;
if (value != 2) {}
int root = (int) if Math.sqrt(value % 2 == 0n) {;
for (int k = 3; k <= root; k += 2) {
if (n % k == 0) {
return false;
}
int truncatedSquareRoot = (int) Math.sqrt(value);
for (int k = 3; k <= truncatedSquareRoot; k += 2)
if (value % k == 0) {
return false;
}
}
return true;
}
}</syntaxhighlight>
{{Output}}
<pre>[2, 3, 5, 7, 13, 17, 19, 23, 29, 37, 47, 59, 67, 79, 89, 127, 137, 139, 149, 157, 167, 179, 239, 257, 269, 347, 349, 359, 367, 379, 389, 457, 467, 479, 569, 1237, 1249, 1259, 1279, 1289, 1367, 1459, 1489, 1567, 1579, 1789, 2347, 2357, 2389, 2459, 2467, 2579, 2689, 2789, 3457, 3467, 3469, 4567, 4679, 4789, 5689, 12347, 12379, 12457, 12479, 12569, 12589, 12689, 13457, 13469, 13567, 13679, 13789, 15679, 23459, 23567, 23689, 23789, 25679, 34589, 34679, 123457, 123479, 124567, 124679, 125789, 134789, 145679, 234589, 235679, 235789, 245789, 345679, 345689, 1234789, 1235789, 1245689, 1456789, 12356789, 23456789]
total time consumed = 4.964799999999999 milliseconds</pre>
 
=={{header|JavaScript}}==
{{trans|Java}}
<syntaxhighlight lang="javascript"><!DOCTYPE html>
<html>
<body>
<noscript>
No script, no fun. Turn on Javascript on.
</noscript>
 
<script>
(()=>{
 
function isPrime(n) {
private void recursive(int m, int value) {
if values.add(valuen == 2);
for (int kreturn = m + 1true; k <= 9; k++)
if (n == 1 || n recursive(k, 10 *% value2 +== k0);
return false;
root = Math.sqrt(n)
for (let k = 3; k <= root; k += 2)
if (n % k == 0)
return false;
return true;
}
 
}
let queue = [];
</lang>
let primes = [];
 
for (let k = 1; k <= 9; k++)
queue.push(k);
 
while (queue.length != 0)
{
let n = queue.shift();
if (isPrime(n))
primes.push(n);
for (let k = n % 10 + 1; k <= 9; k++)
queue.push(n * 10 + k);
}
 
document.writeln(primes);
 
})();
</script>
 
</body>
</html></syntaxhighlight>
{{Output}}
<pre>[2, 3, 5, 7, 13, 17, 19, 23, 29, 37, 47, 59, 67, 79, 89, 127, 137, 139, 149, 157, 167, 179, 239, 257, 269, 347, 349, 359, 367, 379, 389, 457, 467, 479, 569, 1237, 1249, 1259, 1279, 1289, 1367, 1459, 1489, 1567, 1579, 1789, 2347, 2357, 2389, 2459, 2467, 2579, 2689, 2789, 3457, 3467, 3469, 4567, 4679, 4789, 5689, 12347, 12379, 12457, 12479, 12569, 12589, 12689, 13457, 13469, 13567, 13679, 13789, 15679, 23459, 23567, 23689, 23789, 25679, 34589, 34679, 123457, 123479, 124567, 124679, 125789, 134789, 145679, 234589, 235679, 235789, 245789, 345679, 345689, 1234789, 1235789, 1245689, 1456789, 12356789, 23456789]</pre>
total time consumed = 5.207 milliseconds
</pre>
 
=={{header|jq}}==
Line 640 ⟶ 1,248:
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.
 
<syntaxhighlight lang="jq">
<lang jq>
# Output: the stream of ascending primes, in order
def ascendingPrimes:
Line 664 ⟶ 1,272:
( _nwise(10) | map(lpad(10)) | join(" ") );
 
task</langsyntaxhighlight>
{{out}}
<pre>
Line 682 ⟶ 1,290:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
using Primes
 
Line 694 ⟶ 1,302:
@time ascendingprimes()
 
</langsyntaxhighlight>{{out}}
<pre>
2 3 5 7 13 17 19 23 29 37
Line 712 ⟶ 1,320:
=={{header|Lua}}==
Exactly 511 calls to <code>is_prime</code> required.
<langsyntaxhighlight Lualang="lua">local function is_prime(n)
if n < 2 then return false end
if n % 2 == 0 then return n==2 end
Line 735 ⟶ 1,343:
end
 
print(table.concat(ascending_primes(), ", "))</langsyntaxhighlight>
{{out}}
<pre>2, 3, 5, 7, 13, 17, 19, 23, 29, 37, 47, 59, 67, 79, 89, 127, 137, 139, 149, 157, 167, 179, 239, 257, 269, 347, 349, 359, 367, 379, 389, 457, 467, 479, 569, 1237, 1249, 1259, 1279, 1289, 1367, 1459, 1489, 1567, 1579, 1789, 2347, 2357, 2389, 2459, 2467, 2579, 2689, 2789, 3457, 3467, 3469, 4567, 4679, 4789, 5689, 12347, 12379, 12457, 12479, 12569, 12589, 12689, 13457, 13469, 13567, 13679, 13789, 15679, 23459, 23567, 23689, 23789, 25679, 34589, 34679, 123457, 123479, 124567, 124679, 125789, 134789, 145679, 234589, 235679, 235789, 245789, 345679, 345689, 1234789, 1235789, 1245689, 1456789, 12356789, 23456789</pre>
 
=={{header|Matlab}}==
{{trans|Java}}
<syntaxhighlight lang="matlab">queue = 1:9;
 
j = 1;
while j < length(queue)
n = queue(j);
j = j + 1;
a = n * 10 + mod(n, 10) + 1;
b = n * 10 + 9;
if a <= b
queue = [queue, a:b];
end
end
 
queue(isprime(queue))</syntaxhighlight>
 
{{Output}}
<pre>ans =
 
Columns 1 through 8
 
2 3 5 7 13 17 19 23
 
Columns 9 through 16
 
29 37 47 59 67 79 89 127
 
Columns 17 through 24
 
137 139 149 157 167 179 239 257
 
Columns 25 through 32
 
269 347 349 359 367 379 389 457
 
Columns 33 through 40
 
467 479 569 1237 1249 1259 1279 1289
 
Columns 41 through 48
 
1367 1459 1489 1567 1579 1789 2347 2357
 
Columns 49 through 56
 
2389 2459 2467 2579 2689 2789 3457 3467
 
Columns 57 through 64
 
3469 4567 4679 4789 5689 12347 12379 12457
 
Columns 65 through 72
 
12479 12569 12589 12689 13457 13469 13567 13679
 
Columns 73 through 80
 
13789 15679 23459 23567 23689 23789 25679 34589
 
Columns 81 through 88
 
34679 123457 123479 124567 124679 125789 134789 145679
 
Columns 89 through 96
 
234589 235679 235789 245789 345679 345689 1234789 1235789
 
Columns 97 through 100
 
1245689 1456789 12356789 23456789</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ps=Sort@Select[FromDigits /@ Subsets[Range@9, {1, \[Infinity]}], PrimeQ];
Multicolumn[ps, {Automatic, 6}, Appearance -> "Horizontal"]</langsyntaxhighlight>
{{out}}
<pre>2 3 5 7 13 17 19 23
Line 757 ⟶ 1,437:
1245689 1456789 12356789 23456789</pre>
 
=={{header|PerlNim}}==
We build the candidates using a loop by increasing length. Our solution needs only 502 primality tests.
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="Nim">import std/[strutils, sugar]
 
proc isPrime(n: int): bool =
use strict; # https://rosettacode.org/wiki/Ascending_primes
assert n > 7
if n mod 2 == 0 or n mod 3 == 0: return false
var d = 5
var step = 2
while d * d <= n:
if n mod d == 0:
return false
inc d, step
step = 6 - step
result = true
 
iterator ascendingPrimes(): int =
 
# Yield one digit primes.
for n in [2, 3, 5, 7]:
yield n
 
# Yield other primes by increasing length and in ascending order.
type Item = tuple[val, lastDigit: int]
var items: seq[Item] = collect(for n in 1..9: (n, n))
for ndigits in 2..9:
var nextItems: seq[Item]
for item in items:
for newDigit in (item.lastDigit + 1)..9:
let newVal = 10 * item.val + newDigit
nextItems.add (val: newVal, lastDigit: newDigit)
if newVal.isPrime():
yield newVal
items = move(nextItems)
 
 
var rank = 0
for prime in ascendingPrimes():
inc rank
stdout.write ($prime).align(8)
stdout.write if rank mod 10 == 0: '\n' else: ' '
</syntaxhighlight>
{{out}}
<pre> 2 3 5 7 13 17 19 23 29 37
47 59 67 79 89 127 137 139 149 157
167 179 239 257 269 347 349 359 367 379
389 457 467 479 569 1237 1249 1259 1279 1289
1367 1459 1489 1567 1579 1789 2347 2357 2389 2459
2467 2579 2689 2789 3457 3467 3469 4567 4679 4789
5689 12347 12379 12457 12479 12569 12589 12689 13457 13469
13567 13679 13789 15679 23459 23567 23689 23789 25679 34589
34679 123457 123479 124567 124679 125789 134789 145679 234589 235679
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let is_prime n =
let rec test x =
let q = n / x in x > q || x * q <> n && n mod (x + 2) <> 0 && test (x + 6)
in if n < 5 then n lor 1 = 3 else n land 1 <> 0 && n mod 3 <> 0 && test 5
 
let ascending_ints =
let rec range10 m d = if d < 10 then m + d :: range10 m (succ d) else [] in
let up n = range10 (n * 10) (succ (n mod 10)) in
let rec next l = if l = [] then [] else l @ next (List.concat_map up l) in
next [0]
 
let () =
List.filter is_prime ascending_ints
|> List.iter (Printf.printf " %u") |> print_newline</syntaxhighlight>
{{out}}
<pre> 2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789</pre>
 
=={{header|Pascal}}==
{{trans|JavaScript}}
<syntaxhighlight lang="pascal">{$mode Delphi}
 
{ Note that for the program to work properly,
integer variables must be at least 28-bit.
Free Pascal Compiler uses 16-bit integers by default,
so a directive like above is needed. }
 
program ascendingprimes(output);
 
const maxsize = 1000;
 
var
queue, primes : array[1..maxsize] of integer;
b, e, n, k, v : integer;
 
 
function isprime(n: integer): boolean;
 
var
ans : boolean;
root, k : integer;
begin
if n = 2 then
ans := true
else if (n = 1) or (n mod 2 = 0) then
ans := false
else
begin
root := trunc(sqrt(n));
ans := true;
k := 3;
while ans and (k <= root) do
if n mod k = 0 then
ans := false
else
k := k + 2;
end;
isprime := ans
end;
 
begin
 
b := 1;
e := 1;
n := 0;
 
for k := 1 to 9 do
begin
queue[e] := k;
e := e + 1
end;
 
while b < e do
begin
v := queue[b];
b := b + 1;
if isprime(v) then
begin
n := n + 1;
primes[n] := v
end;
 
for k := v mod 10 + 1 to 9 do
begin
queue[e] := v * 10 + k;
e := e + 1
end
 
end;
 
for k := 1 to n do
write(primes[k], ' ');
writeln()
 
end.</syntaxhighlight>
{{Output}}
<pre>2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789 </pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl">
use strict;
use warnings;
use ntheory qw( 'is_prime )';
 
print join( '', map { sprintf "%10d", $_ } sort { $a <=> $b }
map { sprintf '%10d', $_ }
grep /./ && is_prime($_),
sort { $a <=> $b }
glob join '', map "{$_,}", 1 .. 9) =~ s/.{50}\K/\n/gr;</lang>
grep /./ && is_prime $_,
glob join '', map "{$_,}", 1..9
) =~ s/.{50}\K/\n/gr;
</syntaxhighlight>
{{out}}
<pre>
Line 792 ⟶ 1,629:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">ascending_primes</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
Line 805 ⟶ 1,642:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</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;">"%8d"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ascending_primes</span><span style="color: #0000FF;">({</span><span style="color: #000000;">2</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;">"There are %,d ascending primes:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 822 ⟶ 1,659:
===powerset===
Using a powerset, the basic idea of which was taken from the Factor entry above, here incrementally built, does not need either recursion or a sort, same output
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">ascending_primes</span><span style="color: #0000FF;">()</span>
Line 841 ⟶ 1,678:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</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;">"%8d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">ascending_primes</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;">"There are %,d ascending primes:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
By way of explanation, specifically "no sort rqd", if you <code>pp(shorten(powerset,"entries",3))</code> at the end of each iteration then you get:
<pre>
Line 855 ⟶ 1,692:
{}
</pre>
 
=={{header|PHP}}==
{{trans|JavaScript}}
<syntaxhighlight lang="php"><?php
 
function isPrime($n)
{
if ($n == 2)
return true;
if ($n == 1 || $n % 2 == 0)
return false;
$root = intval(sqrt($n));
for ($k = 3; $k <= $root; $k += 2)
if ($n % $k == 0)
return false;
return true;
}
 
$queue = [];
$primes = [];
 
$begin = 0;
$end = 0;
 
for ($k = 1; $k <= 9; $k++)
$queue[$end++] = $k;
 
while ($begin < $end)
{
$n = $queue[$begin++];
 
if (isPrime($n))
$primes[] = $n;
for ($k = $n % 10 + 1; $k <= 9; $k++)
$queue[$end++] = $n * 10 + $k;
}
 
foreach($primes as $p)
echo "$p ";</syntaxhighlight>
{{Output}}
<pre>2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789 </pre>
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">import util.
 
main =>
Line 865 ⟶ 1,743:
end,
nl,
println(len=DP.len)</langsyntaxhighlight>
 
{{out}}
Line 882 ⟶ 1,760:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de prime? (N)
(or
(= N 2)
Line 904 ⟶ 1,782:
(let Fmt (need 10 10)
(while (cut 10 'Lst)
(apply tab @ Fmt) ) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 917 ⟶ 1,795:
34679 123457 123479 124567 124679 125789 134789 145679 234589 235679
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
</pre>
 
=={{header|Prolog}}==
{{works with|swi-prolog}}© 2023<syntaxhighlight lang="prolog">
isPrime(2).
isPrime(N):-
between(3, inf, N),
N /\ 1 > 0, % odd
M is floor(sqrt(N)) - 1, % reverse 2*I+1
Max is M div 2,
forall(between(1, Max, I), N mod (2*I+1) > 0).
 
combi(0, _, Num, Num).
combi(N, [X|T], Acc, Num):-
N > 0,
N1 is N - 1,
Acc1 is Acc * 10 + X,
combi(N1, T, Acc1, Num).
combi(N, [_|T], Acc, Num):-
N > 0,
combi(N, T, Acc, Num).
 
ascPrimes(Num):-
between(1, 9, N),
combi(N, [1, 2, 3, 4, 5, 6, 7, 8, 9], 0, Num),
isPrime(Num).
 
showList(List):-
findnsols(10, DPrim, (member(DPrim, List), writef('%9r', [DPrim])), _),
nl,
fail.
showList(_).
do:-findall(DPrim, ascPrimes(DPrim), DList),
showList(DList).
</syntaxhighlight>
{{out}}
<pre>
?- do.
2 3 5 7 13 17 19 23 29 37
47 59 67 79 89 127 137 139 149 157
167 179 239 257 269 347 349 359 367 379
389 457 467 479 569 1237 1249 1259 1279 1289
1367 1459 1489 1567 1579 1789 2347 2357 2389 2459
2467 2579 2689 2789 3457 3467 3469 4567 4679 4789
5689 12347 12379 12457 12479 12569 12589 12689 13457 13469
13567 13679 13789 15679 23459 23567 23689 23789 25679 34589
34679 123457 123479 124567 124679 125789 134789 145679 234589 235679
235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
true.
</pre>
 
=={{header|Python}}==
===Recursive solution, with a number generator and sorting of results.===
[The <code>isprime</code> here is taken from sympy module, but any of the functions of the same name from other tasks on this site would work.] -- this is a false assumption, e.g. def isprime(n): return "Fat boy" has `the same name` but can not be used here!
 
<lang Python>from sympy import isprime
<syntaxhighlight lang="python">from sympy import isprime
 
def ascending(x=0):
Line 928 ⟶ 1,857:
yield(y)
 
print(sorted(x for x in ascending() if isprime(x)))</langsyntaxhighlight>
{{out}}
<pre>[2, 3, 5, 7, 13, 17, 19, 23, 29, 37, 47, 59, 67, 79, 89, 127, 137, 139, 149, 157, 167, 179, 239, 257, 269, 347, 349, 359, 367, 379, 389, 457, 467, 479, 569, 1237, 1249, 1259, 1279, 1289, 1367, 1459, 1489, 1567, 1579, 1789, 2347, 2357, 2389, 2459, 2467, 2579, 2689, 2789, 3457, 3467, 3469, 4567, 4679, 4789, 5689, 12347, 12379, 12457, 12479, 12569, 12589, 12689, 13457, 13469, 13567, 13679, 13789, 15679, 23459, 23567, 23689, 23789, 25679, 34589, 34679, 123457, 123479, 124567, 124679, 125789, 134789, 145679, 234589, 235679, 235789, 245789, 345679, 345689, 1234789, 1235789, 1245689, 1456789, 12356789, 23456789]</pre>
 
===Queue-based solution that does not need sorting.===
{{trans|Pascal}}
<syntaxhighlight lang="python">def isprime(n):
if n == 2: return True
if n == 1 or n % 2 == 0: return False
root1 = int(n**0.5) + 1;
for k in range(3, root1, 2):
if n % k == 0: return False
return True
 
queue = [k for k in range(1, 10)]
primes = []
 
while queue:
n = queue.pop(0)
if isprime(n):
primes.append(n)
queue.extend(n * 10 + k for k in range(n % 10 + 1, 10))
 
print(primes)</syntaxhighlight>
{{Output}}
<pre>[2, 3, 5, 7, 13, 17, 19, 23, 29, 37, 47, 59, 67, 79, 89, 127, 137, 139, 149, 157, 167, 179, 239, 257, 269, 347, 349, 359, 367, 379, 389, 457, 467, 479, 569, 1237, 1249, 1259, 1279, 1289, 1367, 1459, 1489, 1567, 1579, 1789, 2347, 2357, 2389, 2459, 2467, 2579, 2689, 2789, 3457, 3467, 3469, 4567, 4679, 4789, 5689, 12347, 12379, 12457, 12479, 12569, 12589, 12689, 13457, 13469, 13567, 13679, 13789, 15679, 23459, 23567, 23689, 23789, 25679, 34589, 34679, 123457, 123479, 124567, 124679, 125789, 134789, 145679, 234589, 235679, 235789, 245789, 345679, 345689, 1234789, 1235789, 1245689, 1456789, 12356789, 23456789]</pre>
 
Line 936 ⟶ 1,888:
<code>powerset</code> is defined at [[Power set#Quackery]], and <code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 swap witheach
[ swap 10 * + ] ] is digits->n ( [ --> n )
 
Line 944 ⟶ 1,896:
[ digits->n dup isprime
iff join else drop ]
sort echo</langsyntaxhighlight>
 
{{out}}
Line 952 ⟶ 1,904:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>put (flat 2, 3, 5, 7, sort +*, gather (1..8).map: &recurse ).batch(10)».fmt("%8d").join: "\n";
 
sub recurse ($str) {
Line 959 ⟶ 1,911:
}
 
printf "%.3f seconds", now - INIT now;</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 13 17 19 23 29 37
Line 974 ⟶ 1,926:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">show("ascending primes", sort(cending_primes(seq(1, 9))))
{{incorrect|Ring| <br> Many of the numbers shown do not have strictly ascending digits, e.g. all the ones starting with 9. <br> The single digit primes should also be included. }}
 
func show(title, itm)
<lang ring>
l = len(itm); ? "" + l + " " + title + ":"
load "stdlibcore.ring"
for i = 1 to l
see fmt(itm[i], 9)
if i % 5 = 0 and i < l? "" ok
next : ? ""
 
func seq(b, e)
limit = 1000
res = []; d = e - b
row = 0
s = d / fabs(d)
for i = b to e step s add(res, i) next
return res
 
func ispr(n)
for n = 1 to limit
if n flag< =2 return 0 ok
if n strn& 1 = string(0 return n) = 2 ok
if n if% 3 = 0 return isprime(n) = 13 ok
for ml = 1 to lensqrt(strnn)-1
for f = 5 to l
if number(substr(strn,m)) > number(substr(strn,m+1))
if n % f = 0 or n % (f flag+ 2) = 10 return false ok
next : return ok1
next
if flag = 1
row++
see "" + n + " "
ok
if row % 10 = 0
see nl
ok
ok
next
</lang>
Output:
 
func cending_primes(digs)
11 13 17 19 23 29 31 37 41 43
cand = [0]
47 53 59 61 67 71 73 79 83 89
pr = []
97 101 103 107 109 113 127 131 137 139
for i in digs
149 151 157 163 167 173 179 181 191 193
lcand = cand
197 199 211 223 227 229 233 239 241 251
for j in lcand
257 263 269 271 277 281 283 293 307 311
313 317 331 337 347 349 353v 359= 367j 373* 10 + i
if ispr(v) add(pr, v) ok
379 383 389 397 401 409 419 421 431 433
add(cand, v)
439 443 449 457 461 463 467 479 487 491
next
499 503 509 521 523 541 547 557 563 569
next
571 577 587 593 599 601 607 613 617 619
return pr
631 641 643 647 653 659 661 673 677 683
 
691 701 709 719 727 733 739 743 751 757
func fmt(x, l)
761 769 773 787 797 809 811 821 823 827
829 839 853res 857= 859" 863 877 881 883 887 " + x
return right(res, l)</syntaxhighlight>
907 911 919 929 937 941 947 953 967 971
{{out}}
977 983 991 997
<pre>100 ascending primes:
2 3 5 7 13
17 19 23 29 37
47 59 67 79 89
127 137 139 149 157
167 179 239 257 269
347 349 359 367 379
389 457 467 479 569
1237 1249 1259 1279 1289
1367 1459 1489 1567 1579
1789 2347 2357 2389 2459
2467 2579 2689 2789 3457
3467 3469 4567 4679 4789
5689 12347 12379 12457 12479
12569 12589 12689 13457 13469
13567 13679 13789 15679 23459
23567 23689 23789 25679 34589
34679 123457 123479 124567 124679
125789 134789 145679 234589 235679
235789 245789 345679 345689 1234789
1235789 1245689 1456789 12356789 23456789</pre>
 
=={{header|RPL}}==
Thanks to recursion, we have here a compact and efficient code that generates only ascending odd integers with 9 digits and less, and then check their primality.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ IF DUP 5 ≤ THEN { 2 3 5 } SWAP POS
ELSE
IF DUP 2 MOD NOT THEN 2
ELSE
DUP √ CEIL → lim
≪ 3 WHILE DUP2 MOD OVER lim ≤ AND REPEAT 2 + END
END MOD
END SIGN
≫ 'P'''RIM?'''' STO
SWAP 1 - SWAP
10 * LAST MOD 1 +
'''IF''' 3 PICK NOT '''THEN''' DUP 2 MOD NOT + '''END'''
+ LAST DROP 9 4 PICK - + '''FOR''' d
'''IF''' DUP
'''THEN''' SWAP OVER d '''APRIM''' SWAP
'''ELSE'''
IF d '''PRIM?''' '''THEN''' SWAP d + SWAP '''END'''
d 1 + 'd' STO
'''END'''
'''NEXT''' DROP
≫ ''''APRIM'''' STO
|
'''PRIM?''' ''( n -- boolean)''
'''APRIM''' ''( { } n seed -- { asc } )''
n ← n-1
preparing loop from ##u to ##v, where ## = seed,
u = last digit of seed + 1 (or + 2 if last recursion
and seed odd) ; v = 10 - n
if not last recursion
generate next digits
else
store in list if prime
skip to next odd value
.
forget n
.
|}
 
{{in}}
<pre>
≪ { 2 } 1 9 FOR n n 0 APRIM NEXT ≫ EVAL
</pre>
{{out}}
<pre>
1: { 2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789 }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'prime'
 
digits = [9,8,7,6,5,4,3,2,1]
Line 1,032 ⟶ 2,068:
end
 
puts res.join(",")</langsyntaxhighlight>
{{out}}
<pre>2,3,5,7,13,17,19,23,29,37,47,59,67,79,89,127,137,139,149,157,167,179,239,257,269,347,349,359,367,379,389,457,467,479,569,1237,1249,1259,1279,1289,1367,1459,1489,1567,1579,1789,2347,2357,2389,2459,2467,2579,2689,2789,3457,3467,3469,4567,4679,4789,5689,12347,12379,12457,12479,12569,12589,12689,13457,13469,13567,13679,13789,15679,23459,23567,23689,23789,25679,34589,34679,123457,123479,124567,124679,125789,134789,145679,234589,235679,235789,245789,345679,345689,1234789,1235789,1245689,1456789,12356789,23456789
</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func primes_with_ascending_digits(base = 10) {
 
var list = []
Line 1,065 ⟶ 2,102:
arr.each_slice(10, {|*a|
say a.map { '%8s' % _ }.join(' ')
})</langsyntaxhighlight>
{{out}}
<pre>
Line 1,082 ⟶ 2,119:
</pre>
 
=={{header|VlangSparForte}}==
{{trans|Pascal}}
<syntaxhighlight lang="ada">
#!/usr/local/bin/spar
pragma annotate( summary, "primes_asc" );
pragma annotate( description, "Generate and show all primes with strictly ascending decimal digits" );
pragma annotate( description, "Translation of Pascal" );
pragma annotate( see_also, "https://rosettacode.org/wiki/Ascending_primes" );
pragma annotate( author, "Ken O. Burtch" );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure primes_asc is
maxsize : constant natural := 1000;
 
queue : array(1..maxsize) of natural;
primes: array(1..maxsize) of natural;
 
b : natural;
e : natural;
n : natural;
v : natural;
 
function is_prime(num: integer) return boolean is
found : boolean;
num_root : natural;
k : natural;
begin
if num = 2 then
found;
elsif (num = 1) or (num mod 2 = 0) then
found := false;
else
num_root := numerics.truncation(numerics.sqrt(num));
found;
k := 3;
while found and (k <= num_root) loop
if num mod k = 0 then
found := false;
else
k := @ + 2;
end if;
end loop;
end if;
return found;
end is_prime;
 
begin
b := 1;
e := 1;
n := 0;
 
for k in 1..9 loop
queue(e) := k;
e := e + 1;
end loop;
 
while b < e loop
v := queue(b);
b := @ + 1;
if is_prime(v) then
n := @ + 1;
primes(n) := v;
end if;
 
for k in v mod 10 + 1..9 loop
queue(e) := v * 10 + k;
e := @ + 1;
end loop;
end loop;
 
for k in 1..n loop
put(primes(k), "ZZZZZZZZ9");
if k mod 8 = 0 then
new_line;
end if;
end loop;
new_line;
end primes_asc;</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 13 17 19 23
29 37 47 59 67 79 89 127
137 139 149 157 167 179 239 257
269 347 349 359 367 379 389 457
467 479 569 1237 1249 1259 1279 1289
1367 1459 1489 1567 1579 1789 2347 2357
2389 2459 2467 2579 2689 2789 3457 3467
3469 4567 4679 4789 5689 12347 12379 12457
12479 12569 12589 12689 13457 13469 13567 13679
13789 15679 23459 23567 23689 23789 25679 34589
34679 123457 123479 124567 124679 125789 134789 145679
234589 235679 235789 245789 345679 345689 1234789 1235789
1245689 1456789 12356789 23456789
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|Pascal, C#, C++, C, Fortran}}
<syntaxhighlight lang="vbnet">Module AscendingPrimes
 
Function isPrime(n As Integer)
n = Math.Abs(n)
If n = 2 Then
Return True
End If
If n = 1 Or n Mod 2 = 0 Then
Return False
End If
Dim root As Integer = Math.Sqrt(n)
For k = 3 To root Step 2
If n Mod k = 0 Then
Return False
End If
Next
Return True
End Function
 
 
Sub Main()
 
Dim queue As Queue(Of Integer) = New Queue(Of Integer)
Dim primes As List(Of Integer) = New List(Of Integer)
 
For k = 1 To 9
queue.Enqueue(k)
Next
 
While queue.Count > 0
Dim n As Integer = queue.Dequeue()
If (isPrime(n)) Then
primes.Add(n)
End If
For k = n Mod 10 + 1 To 9
queue.Enqueue(n * 10 + k)
Next
End While
 
For Each p As Integer In primes
Console.Write(p)
Console.Write(" ")
Next
Console.WriteLine()
 
End Sub
 
End Module</syntaxhighlight>
{{Output}}
<pre>2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn is_prime(n int) bool {
if n < 2 {
return false
Line 1,140 ⟶ 2,328:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,163 ⟶ 2,351:
===Version 1 (Sieve)===
Although they use a lot of memory, sieves usually produce good results in Wren and here we only need to sieve for primes up to 3456789 as there are just 9 possible candidates with 8 digits and 1 possible candidate with 9 digits which we can test for primality individually. The following runs in around 0.43 seconds.
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
 
Line 1,188 ⟶ 2,375:
ascPrimes.addAll(higherPrimes)
System.print("There are %(ascPrimes.count) ascending primes, namely:")
Fmt.tprint("$8d", ascPrimes, 10)</syntaxhighlight>
for (chunk in Lst.chunks(ascPrimes, 10)) Fmt.print("$8d", chunk)</lang>
 
{{out}}
Line 1,209 ⟶ 2,396:
 
Much quicker than the 'sieve' approach at 0.013 seconds. I also tried using a powerset but that was slightly slower at 0.015 seconds.
<langsyntaxhighlight ecmascriptlang="wren">import "./set" for Set
import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
 
Line 1,234 ⟶ 2,420:
ascPrimes.sort()
System.print("There are %(ascPrimes.count) ascending primes, namely:")
Fmt.tprint("$8d", ascPrimes, 10)</syntaxhighlight>
for (chunk in Lst.chunks(ascPrimes, 10)) Fmt.print("$8s", chunk)</lang>
 
{{out}}
Line 1,243 ⟶ 2,429:
=={{header|XPL0}}==
Brute force solution: 4.3 seconds on Pi4.
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 1,276 ⟶ 2,462:
if rem(Cnt/10) = 0 then CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
Line 1,294 ⟶ 2,480:
===powerset===
Aaah! Power set, from Factor. Runs in less than 1 millisecond. A better way of measuring duration than using Linux's time utility gave a more credible 35 milliseconds.
<langsyntaxhighlight XPL0lang="xpl0">include xpllib; \provides IsPrime and Sort
 
int I, N, Mask, Digit, A(512), Cnt;
Line 1,318 ⟶ 2,504:
];
];
]</langsyntaxhighlight>
 
{{out}}
51

edits