Jump to content

Descending primes: Difference between revisions

m
syntax highlighting fixup automation
(Added C version)
m (syntax highlighting fixup automation)
Line 14:
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-rows}}
<langsyntaxhighlight lang="algol68">BEGIN # find all primes with strictly decreasing digits #
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</langsyntaxhighlight>
{{out}}
<pre>
Line 73:
=={{header|Arturo}}==
{{trans|ALGOL 68}}
<langsyntaxhighlight lang="rebol">descending: @[
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
]</langsyntaxhighlight>
 
{{out}}
Line 114:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f DESCENDING_PRIMES.AWK
BEGIN {
Line 150:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 167:
=={{header|C}}==
{{trans|C#}}
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
int ispr(unsigned int n) {
Line 190:
}
printf("\n%d descending primes found", c);
}</langsyntaxhighlight>
{{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.
 
<langsyntaxhighlight lang="csharp">using System;
 
class Program {
Line 224:
Console.WriteLine("\n{0} descending primes found", c);
}
}</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 31
Line 248:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
bool ispr(unsigned int n) {
Line 270:
}
printf("\n%d descending primes found", c);
}</langsyntaxhighlight>
{{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#)]
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{out}}
<pre>
Line 287:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: grouping grouping.extras math math.combinatorics
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.</langsyntaxhighlight>
{{out}}
<pre>
Line 309:
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
#include "sort.bas"
 
Line 339:
Next i
Print Using !"\n\nThere are & descending primes."; cant
Sleep</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 31 41 43 53 61 71
Line 356:
Tested on vfxforth and GForth.
 
<langsyntaxhighlight lang="forth">: is-prime? \ n -- f ; \ Fast enough for this application
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 ;</langsyntaxhighlight>
<pre>
descending-primes
Line 411:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 473:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 493:
Compare with [[Ascending_primes#J|Ascending primes]] (focusing on the computational details, rather than the presentation).
 
<langsyntaxhighlight Jlang="j"> extend=: {{ y;y,L:0(1+each i.1-{:y)}}
($~ 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</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
using Primes
Line 509:
foreach(p -> print(rpad(p[2], 10), p[1] % 10 == 0 ? "\n" : ""), enumerate(descendingprimes()))
</langsyntaxhighlight>{{out}}
<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.
<langsyntaxhighlight lang="lua">local function is_prime(n)
if n < 2 then return false end
if n % 2 == 0 then return n==2 end
Line 547:
end
 
print(table.concat(descending_primes(), ", "))</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight Mathematicalang="mathematica">Sort[Select[FromDigits/@Subsets[Range[9,1,-1],{1,\[Infinity]}],PrimeQ]]</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/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;</langsyntaxhighlight>
{{out}}
<pre>
Line 588:
 
=={{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;">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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 629:
</pre>
=== powerset ===
<!--<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;">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>
<!--</langsyntaxhighlight>-->
Output same as the sorted output above, without requiring a sort.
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">import util.
 
main =>
Line 662:
end,
nl,
println(len=DP.len).</langsyntaxhighlight>
 
{{out}}
Line 677:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from sympy import isprime
 
def descending(xs=range(10)):
Line 687:
print(f'{p:9d}', end=' ' if (1 + i)%8 else '\n')
 
print()</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 31 41 43 53
Line 705:
Trivial variation of [[Ascending primes]] task.
 
<syntaxhighlight lang="raku" perl6line>put (flat 2, 3, 5, 7, sort +*, gather (3..9).map: &recurse ).batch(10)».fmt("%8d").join: "\n";
 
sub recurse ($str) {
.take for ($str X~ (1, 3, 7)).grep: { .is-prime && [>] .comb };
recurse $str × 10 + $_ for 2 ..^ $str % 10;
}</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 31 41 43 53 61 71
Line 723:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">show("decending primes", sort(cending_primes(seq(9, 1))))
 
func show(title, itm)
Line 762:
func fmt(x, l)
res = " " + x
return right(res, l)</langsyntaxhighlight>
{{out}}
<pre>87 decending primes:
Line 785:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'prime'
 
digits = [9,8,7,6,5,4,3,2,1].to_a
Line 795:
end
 
puts res.join(",")</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="ruby">func primes_with_descending_digits(base = 10) {
 
var list = []
Line 830:
arr.each_slice(8, {|*a|
say a.map { '%9s' % _ }.join(' ')
})</langsyntaxhighlight>
{{out}}
<pre>
Line 853:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./perm" for Powerset
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)</langsyntaxhighlight>
 
{{out}}
Line 881:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include xpllib; \provides IsPrime and Sort
 
int I, N, Mask, Digit, A(512), Cnt;
Line 905:
];
];
]</langsyntaxhighlight>
 
{{out}}
10,327

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.