簡単 0件解決済み 正答率0%

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.

制約

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

例

例 1
入力:
n = 10

期待される出力:
expected = 55
入力:
long long n = 10;

期待される出力:
long long expected = 55;
入力:
long n = 10;

期待される出力:
long expected = 55;

解説

1 + 2 + … + 10 = 55.

例 2
入力:
n = 0

期待される出力:
expected = 0
入力:
long long n = 0;

期待される出力:
long long expected = 0;
入力:
long n = 0;

期待される出力:
long expected = 0;

解説

No numbers to add, so the sum is 0.

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。