How to resolve the algorithm Sequence of primes by trial division step by step in the C++ programming language
How to resolve the algorithm Sequence of primes by trial division step by step in the C++ programming language
Table of Contents
Problem Statement
Generate a sequence of primes by means of trial division.
Trial division is an algorithm where a candidate number is tested for being a prime by trying to divide it by other numbers. You may use primes, or any numbers of your choosing, as long as the result is indeed a sequence of primes. The sequence may be bounded (i.e. up to some limit), unbounded, starting from the start (i.e. 2) or above some given value. Organize your function as you wish, in particular, it might resemble a filtering operation, or a sieving operation. If you want to use a ready-made is_prime function, use one from the Primality by trial division page (i.e., add yours there if it isn't there already).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sequence of primes by trial division step by step in the C++ programming language
The provided C++ code is designed to find and display all prime numbers within a specified range. Here's a step-by-step explanation :
-
Header Inclusions: •
<math.h>
: Includes mathematical functions. •<iostream>
and<iomanip>
: Used for input/output operations and manipulating output formatting. -
isPrime Function: • This function determines whether a given unsigned integer
u
is prime. • The function first checks ifu
is less than 4. Ifu
is 1, 2, or 3, it returns true. • Ifu
is even (divisible by 2), it returns false. Ifu
is divisible by 3, it also returns false. • It then calculates the square root ofu
, represented byq
, and a counterc
is initialized to 5. • The function checks ifu
is divisible by eitherc
orc + 2
. If it is, it returns false. • It incrementsc
by 6 and continues checking untilc
is greater than or equal toq
. • If none of the checks find factors, the function returns true, indicating thatu
is prime. -
main Function: • Variables: •
mx
is set to 100000000, representing the upper limit of the prime number search range. •wid
is calculated based on the logarithm ofmx
, plus 1. This determines the width used when displaying prime numbers. • Prime Number Search: • The loop starts withu
set to 3, as 2 is already known to be prime. • It iterates through odd numbers up tomx
. • For eachu
, it calls theisPrime
function to check if it's prime. If it is, the prime number is displayed, and the countp
is incremented. • The loop continues untilu
reaches the given limit. • Output: • The program prints the prime numbers found within the specified range, with each number right-aligned within the calculated widthwid
. • It also displays the total number of prime numbers found.
Source code in the cpp programming language
#include <math.h>
#include <iostream>
#include <iomanip>
bool isPrime( unsigned u ) {
if( u < 4 ) return u > 1;
if( /*!( u % 2 ) ||*/ !( u % 3 ) ) return false;
unsigned q = static_cast<unsigned>( sqrt( static_cast<long double>( u ) ) ),
c = 5;
while( c <= q ) {
if( !( u % c ) || !( u % ( c + 2 ) ) ) return false;
c += 6;
}
return true;
}
int main( int argc, char* argv[] )
{
unsigned mx = 100000000,
wid = static_cast<unsigned>( log10( static_cast<long double>( mx ) ) ) + 1;
std::cout << "[" << std::setw( wid ) << 2 << " ";
unsigned u = 3, p = 1; // <- start computing from 3
while( u < mx ) {
if( isPrime( u ) ) { std::cout << std::setw( wid ) << u << " "; p++; }
u += 2;
}
std::cout << "]\n\n Found " << p << " primes.\n\n";
return 0;
}
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the E programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the Fortran programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Lingo programming language
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the ALGOL-M programming language