Descending primes: Difference between revisions
m
syntax highlighting fixup automation
(Added C version) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 14:
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-rows}}
<
PR read "primes.incl.a68" PR # include prime utilities #
PR read "rows.incl.a68" PR # include array utilities #
Line 58:
IF i MOD 10 = 0 THEN print( ( newline ) ) FI
OD
END</
{{out}}
<pre>
Line 73:
=={{header|Arturo}}==
{{trans|ALGOL 68}}
<
loop 1..9 'a [
loop 1..dec a 'b [
Line 99:
loop split.every:10 select descending => prime? 'row [
print map to [:string] row 'item -> pad item 8
]</
{{out}}
Line 114:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f DESCENDING_PRIMES.AWK
BEGIN {
Line 150:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 167:
=={{header|C}}==
{{trans|C#}}
<
int ispr(unsigned int n) {
Line 190:
}
printf("\n%d descending primes found", c);
}</
{{out}}
Same as C#
Line 197:
This task can be accomplished without using nine nested loops, without external libraries, without dynamic arrays, without sorting, without string operations and without signed integers.
<
class Program {
Line 224:
Console.WriteLine("\n{0} descending primes found", c);
}
}</
{{out}}
<pre> 2 3 5 7 31
Line 248:
=={{header|C++}}==
{{trans|C#}}
<
bool ispr(unsigned int n) {
Line 270:
}
printf("\n%d descending primes found", c);
}</
{{out}}
Same as C#
Line 276:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// Descending 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..9->(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>>(>)10),i*10)))([(4,3);(2,1);(8,7)],10)
|>List.concat|>List.sort|>List.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 287:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
math.functions math.primes math.ranges prettyprint sequences
sequences.extras ;
9 1 [a,b] all-subsets [ reverse 0 [ 10^ * + ] reduce-index ]
[ prime? ] map-filter 10 "" pad-groups 10 group simple-table.</
{{out}}
<pre>
Line 309:
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<
#include "sort.bas"
Line 339:
Next i
Print Using !"\n\nThere are & descending primes."; cant
Sleep</
{{out}}
<pre> 2 3 5 7 31 41 43 53 61 71
Line 356:
Tested on vfxforth and GForth.
<
DUP 1 AND IF \ n is odd
DUP 3 DO
Line 394:
: descending-primes
\ Print the descending primes. Call digits with increasing #digits
CR 9 1 DO I 0 10 digits LOOP ;</
<pre>
descending-primes
Line 411:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 473:
}
fmt.Println()
}</
{{out}}
Line 493:
Compare with [[Ascending_primes#J|Ascending primes]] (focusing on the computational details, rather than the presentation).
<
($~ q:@$)(#~ 1 p: ])10#.&>([:~.@;extend each)^:# >:i.9
2 3 31 43 41 431 421 5 53 541 521 5431 61 653 643 641 631 6521 6421 7 73 71 761 751 743 7643 7621 7541 7321
76543 76541 76421 75431 764321 83 863 853 821 8761 8753 8741 8731 8641 8543 8521 8431 87643 87641 87631 87541 87421 86531 876431 865321 8765321 8764321 97 983
971 953 941 9871 9851 9743 9721 9643 9631 9521 9431 9421 98731 98641 98621 98543 98321 97651 96431 94321 987631 987541 986543 975421 9875321 9754321 98765431 98764321 97654321</
=={{header|Julia}}==
<
using Primes
Line 509:
foreach(p -> print(rpad(p[2], 10), p[1] % 10 == 0 ? "\n" : ""), enumerate(descendingprimes()))
</
<pre>
2 3 5 7 31 41 43 53 61 71
Line 524:
=={{header|Lua}}==
Identical to [[Ascending_primes#Lua]] except for the order of <code>digits</code> list.
<
if n < 2 then return false end
if n % 2 == 0 then return n==2 end
Line 547:
end
print(table.concat(descending_primes(), ", "))</
{{out}}
<pre>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|Mathematica}}/{{header|Wolfram Language}}==
<
{{out}}
<pre>{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|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Descending_primes
Line 564:
print join('', sort map { sprintf "%9d", $_ } grep /./ && is_prime($_),
glob join '', map "{$_,}", reverse 1 .. 9) =~ s/.{45}\K/\n/gr;</
{{out}}
<pre>
Line 588:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">descending_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> <span style="color: #000000;">max_digit</span><span style="color: #0000FF;">=</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)</span>
Line 603:
<span style="color: #000000;">j</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;">11</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%8d"</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 descending 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: #000000;">j</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 629:
</pre>
=== powerset ===
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">descending_primes</span><span style="color: #0000FF;">()</span>
Line 650:
<span style="color: #000000;">j</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;">11</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%8d"</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 descending 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: #000000;">j</span><span style="color: #0000FF;">})</span>
<!--</
Output same as the sorted output above, without requiring a sort.
=={{header|Picat}}==
<
main =>
Line 662:
end,
nl,
println(len=DP.len).</
{{out}}
Line 677:
=={{header|Python}}==
<
def descending(xs=range(10)):
Line 687:
print(f'{p:9d}', end=' ' if (1 + i)%8 else '\n')
print()</
{{out}}
<pre> 2 3 5 7 31 41 43 53
Line 705:
Trivial variation of [[Ascending primes]] task.
<syntaxhighlight lang="raku"
sub recurse ($str) {
.take for ($str X~ (1, 3, 7)).grep: { .is-prime && [>] .comb };
recurse $str × 10 + $_ for 2 ..^ $str % 10;
}</
{{out}}
<pre> 2 3 5 7 31 41 43 53 61 71
Line 723:
=={{header|Ring}}==
<
func show(title, itm)
Line 762:
func fmt(x, l)
res = " " + x
return right(res, l)</
{{out}}
<pre>87 decending primes:
Line 785:
=={{header|Ruby}}==
<
digits = [9,8,7,6,5,4,3,2,1].to_a
Line 795:
end
puts res.join(",")</
{{out}}
<pre>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
Line 801:
=={{header|Sidef}}==
<
var list = []
Line 830:
arr.each_slice(8, {|*a|
say a.map { '%9s' % _ }.join(' ')
})</
{{out}}
<pre>
Line 853:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
import "./math" for Int
import "./seq" for Lst
Line 864:
.sort()
System.print("There are %(descPrimes.count) descending primes, namely:")
for (chunk in Lst.chunks(descPrimes, 10)) Fmt.print("$8s", chunk)</
{{out}}
Line 881:
=={{header|XPL0}}==
<
int I, N, Mask, Digit, A(512), Cnt;
Line 905:
];
];
]</
{{out}}
|