- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 Questions & Answers
- 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
- 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 a number can be expressed as a^b in Python
- Check if an integer can be expressed as a sum of two semi-primes 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++
- Check if a number can be expressed as 2^x + 2^y in C++
- Express an odd number as sum of prime numbers in C++
- Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Check if difference of areas of two squares is prime in Python

Advertisements