Problème
Afficher la traductionThe 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.
Contraintes
0 ≤ n ≤ 1,000,000,000. The result fits a signed 64-bit integer.
Exemples
Exemple 1
Entrée :
n = 10
Sortie attendue :
expected = 55
Entrée :
long long n = 10;
Sortie attendue :
long long expected = 55;
Entrée :
long n = 10;
Sortie attendue :
long expected = 55;
Explication
1 + 2 + … + 10 = 55.
Exemple 2
Entrée :
n = 0
Sortie attendue :
expected = 0
Entrée :
long long n = 0;
Sortie attendue :
long long expected = 0;
Entrée :
long n = 0;
Sortie attendue :
long expected = 0;
Explication
No numbers to add, so the sum is 0.