Ascending primes: Difference between revisions

Line 1,799:
 
=={{header|Prolog}}==
for swi-prolog
<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.===
51

edits