Sum of squares P36668


Statement
 

pdf   zip

thehtml

Write a program that reads a natural number n, and prints the result of the following sum:

    12 + 22 + … + (n−1)2 + n2 .

Input

Input consists of a natural number n.

Output

Print the sum of the first n squares: ∑i=1n i2.

Observation

The summation has a closed formula, so a loop would not be necessary to solve this exercise.

Public test cases
  • Input

    3
    

    Output

    14
    
  • Input

    4
    

    Output

    30
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++ Java Python
    User solutions
    C C++ Fortran Haskell Java Lisp PHP Python Rust