def sum_between_1_and_n(n):
return n * (n + 1) // 2
# Пример
n = 15
result = sum_between_1_and_n(n)
print(result) # Вывод: 120
Альтернативный способ (цикл): def sum_between_1_and_n_iterative(n):
total = 0
for i in range(1, n + 1):
total += i
return total
# Пример
n = 15
result = sum_between_1_and_n_iterative(n)
print(result) # Вывод: 120
пример 15 раве 3