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
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 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.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Variables & Data Types Explained
Understand Python variables and core data types (strings, integers, floats, booleans). A complete beginner guide to memory assignment in Python.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python Dictionary Methods
Learn Python dictionary methods. Complete reference guide for key-value pair insertions, retrievals, updates, and checks.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.