Back to Practice Dashboard
Python BasicsEasy

Can a number be expressed as a sum of two prime numbers

Learn how to solve the 'Can a number be expressed as a sum of two prime numbers' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function is_sum_of_two_primes(n) that takes a positive integer n and returns True if n can be expressed as the sum of two prime numbers, and False otherwise.

For example, 10 = 3 + 7 (both prime), so it returns True. Check all possible pairs (p, n-p) where p is prime and n-p is also prime.

Constraints
  • 2 <= n <= 10^4

Examples

Example 1
Input
is_sum_of_two_primes(10)
Output
True
Explanation

10 = 3 + 7. Both 3 and 7 are prime, so True.

Example 2
Input
is_sum_of_two_primes(11)
Output
True
Explanation

11 = 2 + 9? No (9 not prime). 11 = 3 + 8? No. 11 = 5 + 6? No. But wait — we only need one valid pair. Since all fail here... Actually: no valid pair exists with distinct primes, but wait: is_sum_of_two_primes should be False for 11? Let's check: 2+9=11(9 not prime), 3+8(8 not prime), 5+6(6 not prime). Actually False.

Example 3
Input
is_sum_of_two_primes(4)
Output
True
Explanation

4 = 2 + 2. Both are prime, so True.

Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor