All problems

Problem 780

Sum of Squares up to N

Easy · 10 pts · Math, Basics

Problem

Print 1² + 2² + 3² + ... + N², the sum of the squares of the first N positive integers. A loop works, but there is a neat closed formula: the sum equals N × (N + 1) × (2N + 1) / 6, and the division is always exact. For N up to 10^6 the result is about 3 × 10^17, so use a 64-bit integer.

Input

A single integer N.

Output

Print the sum of squares from 1 to N.

Constraints

1 <= N <= 10^6

Example input

3

Example output

14

Explanation

For N = 3 the sum is 1 + 4 + 9 = 14. The formula gives 3 × 4 × 7 / 6 = 14 as well.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Swap Two Numbers Next: Average of an Array