ง่าย 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.

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก