How to resolve the algorithm Sequence of primes by trial division step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

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 :

  1. Header Inclusions: • <math.h>: Includes mathematical functions. • <iostream> and <iomanip>: Used for input/output operations and manipulating output formatting.

  2. isPrime Function: • This function determines whether a given unsigned integer u is prime. • The function first checks if u is less than 4. If u is 1, 2, or 3, it returns true. • If u is even (divisible by 2), it returns false. If u is divisible by 3, it also returns false. • It then calculates the square root of u, represented by q, and a counter c is initialized to 5. • The function checks if u is divisible by either c or c + 2. If it is, it returns false. • It increments c by 6 and continues checking until c is greater than or equal to q. • If none of the checks find factors, the function returns true, indicating that u is prime.

  3. 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 of mx, plus 1. This determines the width used when displaying prime numbers. • Prime Number Search: • The loop starts with u set to 3, as 2 is already known to be prime. • It iterates through odd numbers up to mx. • For each u, it calls the isPrime function to check if it's prime. If it is, the prime number is displayed, and the count p is incremented. • The loop continues until u reaches the given limit. • Output: • The program prints the prime numbers found within the specified range, with each number right-aligned within the calculated width wid. • 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