How to resolve the algorithm Additive primes step by step in the C++ programming language
How to resolve the algorithm Additive primes step by step in the C++ programming language
Table of Contents
Problem Statement
In mathematics, additive primes are prime numbers for which the sum of their decimal digits are also primes.
Write a program to determine (and show here) all additive primes less than 500. Optionally, show the number of additive primes.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Additive primes step by step in the C++ programming language
The provided code in C++ is designed to find and display all the additive primes less than a specified limit. An additive prime is a prime number whose digits sum is also a prime number.
Here's a detailed explanation of the code:
-
Header Includes:
- The code includes the necessary headers:
<iomanip>
for formatting output<iostream>
for input/output operations
- The code includes the necessary headers:
-
Function
is_prime
:- This function checks if a given unsigned integer
n
is a prime number. - It follows the common prime checking algorithm:
- If
n
is less than 2, it returns false (as 0 and 1 are not prime). - If
n
is even (except for 2), it returns false (as even numbers except 2 are not prime). - If
n
is divisible by 3, it returns false (except for 3 itself). - It then iterates through odd numbers starting from 5 up to the square root of
n
. It checks ifn
is divisible by any of these odd numbers. If it finds a divisor, it returns false. - If no divisors are found, it returns true, indicating that
n
is a prime number.
- If
- This function checks if a given unsigned integer
-
Function
digit_sum
:- This function calculates the sum of the digits of a given unsigned integer
n
. - It initializes a variable
sum
to 0 and repeatedly takes the last digit ofn
(usingn % 10
) and adds it tosum
. - It continues this process until
n
becomes 0, and then returns the calculatedsum
.
- This function calculates the sum of the digits of a given unsigned integer
-
main
Function:- The
main
function is the entry point of the program. - It defines a constant
limit
with the value 500, indicating that the program will search for additive primes less than 500. - It prints a header message indicating the search range.
- It initializes a variable
count
to 0, which will keep track of the number of additive primes found. - It starts a loop that iterates through unsigned integers from 1 to
limit - 1
. - Inside the loop:
- For each
n
, it checks if bothn
and the sum of its digits (calculated usingdigit_sum(n)
) are prime numbers using theis_prime
function. - If both
n
and its digit sum are prime, it meansn
is an additive prime. - It prints
n
with a width of 3 characters on the console. - It increments the
count
variable to keep track of the number of additive primes found. - After printing 10 additive primes on a line, it inserts a newline.
- For each
- After the loop finishes, it prints the total number of additive primes found.
- The
The program continues to find and display all additive primes less than 500, showcasing the fascinating mathematical property of numbers where both the number and the sum of its digits are prime.
This C++ program finds and prints the additive primes less than a given limit. An additive prime is a prime number whose digits also sum up to a prime number.
Here's a detailed explanation of the code:
-
Header Files:
- The code includes
<iomanip>
for manipulating output formatting and<iostream>
for input and output operations.
- The code includes
-
Function
is_prime
:- This function checks whether a given unsigned integer
n
is a prime number. - It uses a basic primality test:
- If
n
is less than 2, it returns false. - It checks if
n
is divisible by 2 or 3. If it is, it returns true only for 2 and 3, respectively. - It then iterates through potential divisors from 5 onward, increasing by 4 (to skip even divisors).
- If it finds any divisors, it returns false.
- If it reaches the end of the loop without finding any divisors, it returns true.
- If
- This function checks whether a given unsigned integer
-
Function
digit_sum
:- This function calculates the sum of the digits of a given unsigned integer
n
. - It iterates through the digits of
n
(by repeatedly dividing by 10 and taking the remainder) and adds them to the sum.
- This function calculates the sum of the digits of a given unsigned integer
-
Main Function (
main
):- It defines a constant
limit
to specify the limit for finding additive primes. - It uses a loop to iterate through integers from 1 to
limit - 1
. - For each integer
n
, it checks if bothn
and the sum of its digits (digit_sum(n)
) are prime numbers using theis_prime
function. - If both conditions are met, it prints
n
usingstd::setw(3) << n
to format the output with a column width of 3 characters. - It keeps track of the count of additive primes and prints a newline after every 10 primes.
- Finally, it prints the total count of additive primes found.
- It defines a constant
Source code in the cpp programming language
#include <iomanip>
#include <iostream>
bool is_prime(unsigned int n) {
if (n < 2)
return false;
if (n % 2 == 0)
return n == 2;
if (n % 3 == 0)
return n == 3;
for (unsigned int p = 5; p * p <= n; p += 4) {
if (n % p == 0)
return false;
p += 2;
if (n % p == 0)
return false;
}
return true;
}
unsigned int digit_sum(unsigned int n) {
unsigned int sum = 0;
for (; n > 0; n /= 10)
sum += n % 10;
return sum;
}
int main() {
const unsigned int limit = 500;
std::cout << "Additive primes less than " << limit << ":\n";
unsigned int count = 0;
for (unsigned int n = 1; n < limit; ++n) {
if (is_prime(digit_sum(n)) && is_prime(n)) {
std::cout << std::setw(3) << n;
if (++count % 10 == 0)
std::cout << '\n';
else
std::cout << ' ';
}
}
std::cout << '\n' << count << " additive primes found.\n";
}
You may also check:How to resolve the algorithm Formal power series step by step in the Raku programming language
You may also check:How to resolve the algorithm File extension is in extensions list step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the Sidef programming language
You may also check:How to resolve the algorithm Web scraping step by step in the C++ programming language
You may also check:How to resolve the algorithm String case step by step in the C# programming language