Đề bài
Xem bản dịchThe 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.
Ràng buộc
0 ≤ n ≤ 1,000,000,000. The result fits a signed 64-bit integer.
Ví dụ
Ví dụ 1
Đầu vào:
n = 10
Kết quả mong đợi:
expected = 55
Đầu vào:
long long n = 10;
Kết quả mong đợi:
long long expected = 55;
Đầu vào:
long n = 10;
Kết quả mong đợi:
long expected = 55;
Giải thích
1 + 2 + … + 10 = 55.
Ví dụ 2
Đầu vào:
n = 0
Kết quả mong đợi:
expected = 0
Đầu vào:
long long n = 0;
Kết quả mong đợi:
long long expected = 0;
Đầu vào:
long n = 0;
Kết quả mong đợi:
long expected = 0;
Giải thích
No numbers to add, so the sum is 0.