Python Nozioni di baseFacile

Somma dei primi N numeri naturali

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

Dichiarazione del problema

Facile

Scrivi una funzione sum_natural(n) che accetta un intero positivo n e restituisce la somma dei primi n numeri naturali, ovvero 1 + 2 + 3 + ... + n.

Vincoli
  • 1 <= n <= 10^6

Esempi

Example 1
Input
n = 5
Output
15
Explanation

1 + 2 + 3 + 4 + 5 = 15.

Example 2
Input
n = 10
Output
55
Explanation

1 + 2 + 3 + ... + 10 = 55.

Example 3
Input
n = 1
Output
1
Explanation

The sum of the first 1 natural number is just 1.

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.