Python Nozioni di baseFacile

Un numero può essere espresso come somma di due numeri primi

Guida dettagliata e implementazione Python per il problema "Un numero può essere espresso come somma di due numeri primi".

Dichiarazione del problema

Facile

Scrivi una funzione is_sum_of_two_primes(n) che accetta un intero positivo n e restituisce True se n può essere espresso come la somma di due numeri primi e False altrimenti.

Ad esempio, 10 = 3 + 7 (entrambi primi), quindi restituisce Vero. Controlla tutte le possibili coppie (p, n-p) dove p è primo e anche n-p è primo.

Vincoli
  • 2 <= n <= 10^4

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Numbers come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.