Python Nozioni di baseFacile

Somma di numeri in un dato intervallo

Guida dettagliata e implementazione Python per il problema "Somma di numeri in un dato intervallo".

Dichiarazione del problema

Facile

Scrivi una funzione sum_range(a, b) che accetta due numeri interi a e b (dove a <= b) e restituisce la somma di tutti i numeri interi da a a b compreso, ovvero a + (a+1) + ... + b.

Vincoli
  • -10^6 <= a <= b <= 10^6

Esempi

Example 1
Input
a = 3, b = 7
Output
25
Explanation

3 + 4 + 5 + 6 + 7 = 25.

Example 2
Input
a = 1, b = 10
Output
55
Explanation

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

Example 3
Input
a = 5, b = 5
Output
5
Explanation

Only one number in the range, so the sum is 5.

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.