How to resolve the algorithm Additive primes step by step in the Python programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Additive primes step by step in the Python 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 Python programming language
Overview:
This Python code searches for "additive primes" below 500 and prints them. An additive prime is a prime number whose digits also form a prime number.
Detailed Explanation:
-
is_prime
Function:- This function checks if a given integer
n
is prime using the Sieve of Eratosthenes. - It handles the base cases (n <= 3) and eliminates even numbers.
- It then iterates through potential prime divisors, using the optimizations that prime divisors come in the form of 6k +/- 1.
- If
n
is divisible by any of the divisors, it returnsFalse
, indicating thatn
is not prime. Otherwise, it returnsTrue
.
- This function checks if a given integer
-
digit_sum
Function:- This helper function calculates the sum of the digits of an integer
n
. - It loops through each digit in
n
by taking the modulo of 10, adding it to the sum, and dividingn
by 10. - It returns the total sum of the digits.
- This helper function calculates the sum of the digits of an integer
-
main
Function:- The
main
function iterates through integers from 2 to 499 to search for additive primes. - For each
i
, it checks if it's prime usingis_prime
. - It also checks if the sum of the digits of
i
is a prime number usingdigit_sum
andis_prime
. - If both conditions are met,
i
is an additive prime, it is printed, and the count of additive primes is incremented. - Finally, it prints the total count of additive primes less than 500.
- The
-
if __name__ == "__main__":
Block:- This is a standard Python idiom that ensures the
main
function is only called when the script is run as the main program (not imported).
- This is a standard Python idiom that ensures the
Source code in the python programming language
def is_prime(n: int) -> bool:
if n <= 3:
return n > 1
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i ** 2 <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def digit_sum(n: int) -> int:
sum = 0
while n > 0:
sum += n % 10
n //= 10
return sum
def main() -> None:
additive_primes = 0
for i in range(2, 500):
if is_prime(i) and is_prime(digit_sum(i)):
additive_primes += 1
print(i, end=" ")
print(f"\nFound {additive_primes} additive primes less than 500")
if __name__ == "__main__":
main()
You may also check:How to resolve the algorithm String interpolation (included) step by step in the OCaml programming language
You may also check:How to resolve the algorithm Truncate a file step by step in the Action! programming language
You may also check:How to resolve the algorithm Priority queue step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Use another language to call a function step by step in the Fortran programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Scheme programming language