Python BasicsEasy

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

Detailed guide and Python implementation for the 'Can a number be expressed as a sum of two prime numbers' problem.

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?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

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

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.