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
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.
- •2 <= n <= 10^4
Examples
is_sum_of_two_primes(10)
True
10 = 3 + 7. Both 3 and 7 are prime, so True.
is_sum_of_two_primes(11)
True
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.
is_sum_of_two_primes(4)
True
4 = 2 + 2. Both are prime, so True.
Need a Hint?
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.