Tutte le sfide
Facile 0 risolte 0% di accettazione

Triangular Number

Add the integers from 1 through n.

The n-th triangular number is 1 + 2 + … + n. A formula avoids looping over huge inputs. The 0th triangular number is 0.

Implement triangular_number(n).

Input

n: A non-negative integer.

Output

Return 1 + 2 + … + n.

Vincoli

0 ≤ n ≤ 1,000,000,000. The result fits a signed 64-bit integer.

Esempi

Esempio 1
Input:
n = 10

Output atteso:
expected = 55
Input:
long long n = 10;

Output atteso:
long long expected = 55;
Input:
long n = 10;

Output atteso:
long expected = 55;

Spiegazione

1 + 2 + … + 10 = 55.

Esempio 2
Input:
n = 0

Output atteso:
expected = 0
Input:
long long n = 0;

Output atteso:
long long expected = 0;
Input:
long n = 0;

Output atteso:
long expected = 0;

Spiegazione

No numbers to add, so the sum is 0.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.