How to resolve the algorithm Descending primes step by step in the C# programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Descending primes step by step in the C# programming language
Table of Contents
Problem Statement
Generate and show all primes with strictly descending decimal digits.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Descending primes step by step in the C# programming language
The source code provides a program that displays a list of descending prime numbers and counts them while generating. It uses a modified Sieve of Eratosthenes algorithm to efficiently generate prime numbers.
Key implementation details:
-
ispr() Function: This function checks if a given number
n
is prime. It follows the common prime checking logic:- If
n
is even (except for 2), it's not prime. - If
n
is greater than 2, it checks for divisibility by odd numbers from 3 to the square root ofn
. If any divisor is found, it returns false. Otherwise, it returns true.
- If
-
Main Logic:
- The program maintains an array
ps
to store the set of prime numbers and uses another arraynxt
to generate the next set of potential prime numbers. - It initializes
ps
with the first few prime numbers (1-9). - The outermost
while
loop continues generating prime numbers until no more candidates can be found. - Inside the loop, it iterates through the current prime numbers in
ps
and filters out the ones that are prime. - For each prime
a
, it appends numbers tonxt
that are multiples ofa
shifted by 10 (e.g., 20, 30, 40, ...). These numbers are placed in the unused slots at the end ofnxt
. - After processing all the primes in
ps
, it checks ifnxt
has any unused slots. If it does, it means no more primes can be generated, and the loop breaks. Otherwise,nxt
is copied intops
, and the process repeats with the expanded set of potential prime numbers.
- The program maintains an array
-
Output:
- The program prints prime numbers in descending order, formatted with 8 characters per number.
- It also counts and displays the total number of descending primes found during the execution.
Source code in the csharp programming language
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} descending primes found", c);
}
}
You may also check:How to resolve the algorithm Arithmetic/Rational step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Peano curve step by step in the C++ programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the REBOL programming language
You may also check:How to resolve the algorithm Apply a digital filter (direct form II transposed) step by step in the zkl programming language
You may also check:How to resolve the algorithm Sort a list of object identifiers step by step in the Racket programming language