- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Check if a prime number can be expressed as sum of two Prime Numbers in Python

Suppose we have a prime number n. we have to check whether we can express n as x + y where x and y are also two prime numbers.

So, if the input is like n = 19, then the output will be True as we can express it like 19 = 17 + 2

To solve this, we will follow these steps −

- Define a function isPrime() . This will take number
- if number <= 1, then
- return False

- if number is same as 2, then
- return True

- if number is even, then
- return False

- for i in range 3 to integer part of ((square root of number) + 1), increase by 2, do
- if number is divisible by i, then
- return False

- if number is divisible by i, then
- return True
- From the main method do the following −
- if isPrime(number) and isPrime(number - 2) both are true, then
- return True

- otherwise,
- return False

Let us see the following implementation to get better understanding −

## Example

from math import sqrt def isPrime(number): if number <= 1: return False if number == 2: return True if number % 2 == 0: return False for i in range(3, int(sqrt(number))+1, 2): if number%i == 0: return False return True def solve(number): if isPrime(number) and isPrime(number - 2): return True else: return False n = 19 print(solve(n))

## Input

19

## Output

True

- Related Articles
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Swift program to check whether a number can be expressed as sum of two prime numbers
- Haskell Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- C program for a number to be expressed as a sum of two prime numbers.
- Check if a number can be expressed as sum two abundant numbers in C++
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Check if an integer can be expressed as a sum of two semi-primes in Python
- Check if a number can be expressed as a^b in Python
- Check if a number can be expressed as power in C++
- Check if a number can be expressed as a^b in C++
- List at least three different ways in which 67 be expressed as the sum of three different prime numbers
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Express an odd number as sum of prime numbers in C++
- Check if a number can be expressed as 2^x + 2^y in C++

Advertisements