Descending primes: Difference between revisions

Added C# version
(→‎{{header|Ring}}: Rewrote code, removed warning)
(Added C# version)
Line 164:
1-99999999: 87 descending primes
</pre>
 
=={{header|C#|CSharp}}==
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.
 
<lang csharp>using System;
 
class Program {
 
static bool ispr(uint n) {
if ((n & 1) == 0 || n < 2) return n == 2;
for (uint j = 3; j * j <= n; j += 2)
if (n % j == 0) return false; return true; }
 
static void Main(string[] args) {
uint c = 0; int nc;
var ps = new uint[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var nxt = new uint[128];
while (true) {
nc = 0;
foreach (var a in ps) {
if (ispr(a))
Console.Write("{0,8}{1}", a, ++c % 5 == 0 ? "\n" : " ");
for (uint b = a * 10, l = a % 10 + b++; b < l; b++)
nxt[nc++] = b;
}
if (nc > 1) {
Array.Resize (ref ps, nc); Array.Copy(nxt, ps, nc); }
else break;
}
Console.WriteLine("\n{0} decending primes found", c); c = 0;
}
}</lang>
{{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
87 decending primes found</pre>
 
=={{header|F_Sharp|F#}}==