Descending primes: Difference between revisions

m
(→‎J: simplify, speedup 10 times)
 
(11 intermediate revisions by 4 users not shown)
Line 549:
Descending Primes Found: 87
</pre>
 
=={{header|EasyLang}}==
<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 nextdesc n . .
if isprim n = 1
write n & " "
.
if n > 987654321
return
.
for d = n mod 10 - 1 downto 1
nextdesc n * 10 + d
.
.
for i = 9 downto 1
nextdesc i
.
</syntaxhighlight>
 
 
=={{header|F_Sharp|F#}}==
Line 561 ⟶ 593:
2 3 5 7 31 41 43 53 61 71 73 83 97 421 431 521 541 631 641 643 653 743 751 761 821 853 863 941 953 971 983 5431 6421 6521 7321 7541 7621 7643 8431 8521 8543 8641 8731 8741 8753 8761 9421 9431 9521 9631 9643 9721 9743 9851 9871 75431 76421 76541 76543 86531 87421 87541 87631 87641 87643 94321 96431 97651 98321 98543 98621 98641 98731 764321 865321 876431 975421 986543 987541 987631 8764321 8765321 9754321 9875321 97654321 98764321 98765431
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
Line 837 ⟶ 870:
9!:37 (512) 1} 9!:36 ''
 
(#~ 1&p:) (#: }. i. 512) 10&#.@# |. >: i. 9_9
2 3 31 41 421 43 431 5 521 53 541 5431 61 631 641 6421 643 6521 653 7 71 73 7321 743 751 7541 75431 761 7621 76421 7643 764321 76541 76543 821 83 8431 8521 853 8543 863 8641 86531 865321 8731 8741 87421 8753 87541 8761 87631 87641 87643 876431 8764321 8765321 941 9421 9431 94321 9521 953 9631 9643 96431 97 971 9721 9743 975421 9754321 97651 97654321 983 98321 9851 98543 98621 98641 986543 9871 98731 9875321 987541 987631 98764321 98765431</syntaxhighlight>
 
Line 1,203 ⟶ 1,236:
8764321 8765321 9754321 9875321 97654321 98764321 98765431
len = 87</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, _, []).
combi(N, [_|T], Comb):-
N > 0,
combi(N, T, Comb).
combi(N, [X|T], [X|Comb]):-
N > 0,
N1 is N - 1,
combi(N1, T, Comb).
 
descPrimes(Num):-
between(1, 9, N),
combi(N, [9, 8, 7, 6, 5, 4, 3, 2, 1], CList),
atomic_list_concat(CList, Tmp), % swi specific
atom_number(Tmp, Num), % int_list_to_number
isPrime(Num).
 
showList(List):-
findnsols(10, DPrim, (member(DPrim, List), writef('%9r', [DPrim])), _),
nl,
fail.
showList(_).
do:-findall(DPrim, descPrimes(DPrim), DList),
showList(DList).
</syntaxhighlight>
{{out}}
<pre>
?- do.
2 3 5 7 31 41 43 53 61 71
73 83 97 421 431 521 541 631 641 643
653 743 751 761 821 853 863 941 953 971
983 5431 6421 6521 7321 7541 7621 7643 8431 8521
8543 8641 8731 8741 8753 8761 9421 9431 9521 9631
9643 9721 9743 9851 9871 75431 76421 76541 76543 86531
87421 87541 87631 87641 87643 94321 96431 97651 98321 98543
98621 98641 98731 764321 865321 876431 975421 986543 987541 987631
8764321 8765321 9754321 9875321 97654321 98764321 98765431
true.
</pre>
 
=={{header|Python}}==
Line 1,417 ⟶ 1,499:
{{libheader|Wren-perm}}
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./perm" for Powerset
import "./math" for Int
import "./seq" for Lst
Line 1,430 ⟶ 1,511:
.sort()
System.print("There are %(descPrimes.count) descending primes, namely:")
for (chunk in Lst.chunks(descPrimes, 10)) Fmt.printtprint("$8s", chunkdescPrimes, 10)</syntaxhighlight>
 
{{out}}
51

edits