Python Nozioni di baseFacile

Numero primo

Guida dettagliata e implementazione Python per il problema dei "numeri primi".

Dichiarazione del problema

Facile

Scrivi una funzione is_prime(n) che accetta un intero positivo n e restituisce True se n è un numero primo, o False altrimenti. Un numero primo è un numero maggiore di 1 che non ha divisori diversi da 1 e se stesso.

Vincoli
  • 1 <= n <= 10^6

Esempi

Example 1
Input
n = 7
Output
True
Explanation

7 is only divisible by 1 and 7, so it is prime.

Example 2
Input
n = 4
Output
False
Explanation

4 is divisible by 2 (4 = 2 × 2), so it is not prime.

Example 3
Input
n = 1
Output
False
Explanation

1 is not considered a prime number by definition.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Basics 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.