How to resolve the algorithm Descending primes step by step in the C programming language
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 code you wrote in C is used to find all the prime numbers that are lower than a given number.
It uses a sieve algorithm that starts with a list of all the prime numbers less than a given number, pc
, and then repeatedly removes all the multiples of each prime number in the list.
First, the function ispr
checks if a given number is prime.
If it is not, it returns 0.
If it is, it returns 1.
The main function then initializes a list of prime numbers, ps
, with the first pc
prime numbers.
It then iterates through all the prime numbers in ps
and prints them, incrementing the count of prime numbers found, c
.
It then iterates through all the multiples of each prime number in ps
and removes them from the list.
The function nxt
is a helper function that generates a list of all the multiples of a given prime number.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function ispr
is a helper function that checks if a given number is prime.
If it is not, it returns 0.
If it is, it returns 1.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function nxt
is a helper function that generates a list of all the multiples of a given prime number.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function ispr
is a helper function that checks if a given number is prime.
If it is not, it returns 0.
If it is, it returns 1.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function nxt
is a helper function that generates a list of all the multiples of a given prime number.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function ispr
is a helper function that checks if a given number is prime.
If it is not, it returns 0.
If it is, it returns 1.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function nxt
is a helper function that generates a list of all the multiples of a given prime number.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function ispr
is a helper function that checks if a given number is prime.
If it is not, it returns 0.
If it is, it returns 1.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function nxt
is a helper function that generates a list of all the multiples of a given prime number.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function ispr
is a helper function that checks if a given number is prime.
If it is not, it returns 0.
If it is, it returns 1.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function nxt
is a helper function that generates a list of all the multiples of a given prime number.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function ispr
is a helper function that checks if a given number is prime.
If it is not, it returns 0.
If it is, it returns 1.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function nxt
is a helper function that generates a list of all the multiples of a given prime number.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function ispr
is a helper function that checks if a given number is prime.
If it is not, it returns 0.
If it is, it returns 1.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function nxt
is a helper function that generates a list of all the multiples of a given prime number.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function ispr
is a helper function that checks if a given number is prime.
If it is not, it returns 0.
If it is, it returns 1.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function nxt
is a helper function that generates a list of all the multiples of a given prime number.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function ispr
is a helper function that checks if a given number is prime.
If it is not, it returns 0.
If it is, it returns 1.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function nxt
is a helper function that generates a list of all the multiples of a given prime number.
The loop then iterates through the list of prime numbers and prints them, incrementing the count of prime numbers found, c
.
The function ispr
is a helper function that checks if a given number is prime.
If it is not, it
Source code in the c programming language
#include <stdio.h>
int ispr(unsigned int n) {
if ((n & 1) == 0 || n < 2) return n == 2;
for (unsigned int j = 3; j * j <= n; j += 2)
if (n % j == 0) return 0; return 1; }
int main() {
unsigned int c = 0, nc, pc = 9, i, a, b, l,
ps[128], nxt[128];
for (a = 0, b = 1; a < pc; a = b++) ps[a] = b;
while (1) {
nc = 0;
for (i = 0; i < pc; i++) {
if (ispr(a = ps[i]))
printf("%8d%s", a, ++c % 5 == 0 ? "\n" : " ");
for (b = a * 10, l = a % 10 + b++; b < l; b++)
nxt[nc++] = b;
}
if (nc > 1) for(i = 0, pc = nc; i < pc; i++) ps[i] = nxt[i];
else break;
}
printf("\n%d descending primes found", c);
}
You may also check:How to resolve the algorithm Dinesman's multiple-dwelling problem step by step in the Haskell programming language
You may also check:How to resolve the algorithm Factorial step by step in the Quackery programming language
You may also check:How to resolve the algorithm N-smooth numbers step by step in the Java programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the Prolog programming language
You may also check:How to resolve the algorithm String length step by step in the Frink programming language