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

Difference of Squares

Subtract the sum of squares from the square of the sum.

Using the integers from 1 through n, calculate two quantities: first add the integers and square the result; separately square each integer and add those squares. Return the first quantity minus the second.

Implement difference_of_squares(n).

Input

n: The last integer to include.

Output

Return square-of-sum minus sum-of-squares.

制約

0 ≤ n ≤ 50,000. For n = 0, both sums are 0. All required values fit signed 64-bit integers.

例

例 1
入力:
n = 4

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

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

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

解説

(1 + 2 + 3 + 4)² = 100. The sum of squares is 1 + 4 + 9 + 16 = 30. Subtract: 100 − 30 = 70.

例 2
入力:
n = 1

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

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

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

解説

Both quantities equal 1, so their difference is 0.

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

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