Sum of square-sums of first n natural numbers

01 Sep, 2021
Given a positive integer n. The task is to find the sum of the sum of square of first n natural number. Examples :  
Input : n = 3
Output : 20
Sum of square of first natural number = 1
Sum of square of first two natural number = 1^2 + 2^2 = 5
Sum of square of first three natural number = 1^2 + 2^2 + 3^2 = 14
Sum of sum of square of first three natural number = 1 + 5 + 14 = 20

Input : n = 2
Output : 6
 
Method 1: O(n) The idea is to find sum of square of first i natural number, where 1 <= i <= n. And them add them all. We can find sum of squares of first n natural number by formula: n * (n + 1)* (2*n + 1)/6Below is the implementation of this approach:  
C++
Java
Python3
C#
PHP
Javascript
// CPP Program to find the sum of sum of// squares of first n natural number#include <bits/stdc++.h>using namespace std; // Function to find sum of sum of square of// first n natural numberint findSum(int n){    int sum = 0;    for (int i = 1; i <= n; i++)        sum += ((i * (i + 1) * (2 * i + 1)) / 6);    return sum;} // Driven Programint main(){    int n = 3;    cout << findSum(n) << endl;    return 0;}
// Java Program to find the sum of// sum of squares of first n natural// number class GFG {         // Function to find sum of sum of    // square of first n natural number    static int findSum(int n)    {        int sum = 0;        for (int i = 1; i <= n; i++)            sum += ((i * (i + 1)               * (2 * i + 1)) / 6);        return sum;    }         // Driver Program    public static void main(String[] args)    {        int n = 3;             System.out.println( findSum(n));    }}  // This code is contributed by// Arnab Kundu
# Python3 Program to find the sum# of sum of squares of first n# natural number # Function to find sum of sum of# square of first n natural numberdef findSum(n):    summ = 0    for i in range(1, n+1):        summ = (summ + ((i * (i + 1)                * (2 * i + 1)) / 6))    return summ # Driven Programn = 3print(int(findSum(n)))  # This code is contributed by# Prasad Kshirsagar
// C# Program to find the sum of sum of// squares of first n natural numberusing System; public class GFG {         // Function to find sum of sum of    // square of first n natural number    static int findSum(int n)    {        int sum = 0;        for (int i = 1; i <= n; i++)            sum += ((i * (i + 1) *                  (2 * i + 1)) / 6);        return sum;    }         // Driver Program    static public void Main()    {        int n = 3;                 Console.WriteLine(findSum(n));    }} // This code is contributed by// Arnab Kundu.
<?php// PHP Program to find the sum of// squares of first n natural number // Function to find sum of sum of// square of first n natural numberfunction findSum( $n){    $sum = 0;    for ($i = 1; $i <= $n; $i++)        $sum += (($i * ($i + 1) *                 (2 * $i + 1)) / 6);    return $sum;} // Driver Code$n = 3;echo findSum($n) ; // This code is contributed by anuj_67.?>
<script> // Javascript Program to find the sum of sum of// squares of first n natural number // Function to find sum of sum of square// of first n natural numberfunction findSum(n){    return (n * (n + 1) * (n + 1) * (n + 2)) / 12;} // Driven Program     let n = 3;    document.write(findSum(n) + "<br>");  // This code is contributed by Mayank Tyagi </script>
20
 
Time Complexity : O(n) Auxiliary Space : O(1)Method 2: O(1) Mathematically, we need to find, Σ ((i * (i + 1) * (2*i + 1)/6), where 1 <= i <= n So, lets solve this summation,  
Sum = Σ ((i * (i + 1) * (2*i + 1)/6), where 1 <= i <= n
    = (1/6)*(Σ ((i * (i + 1) * (2*i + 1)))
    = (1/6)*(Σ ((i2 + i) * (2*i + 1))
    = (1/6)*(Σ ((2*i3 + 3*i2 + i))
    = (1/6)*(Σ 2*i3 + Σ 3*i2 + Σ i)
    = (1/6)*(2*Σ i3 + 3*Σ i2 + Σ i)
    = (1/6)*(2*(i*(i + 1)/2)2 + 3*(i * (i + 1) * (2*i + 1))/6 + i * (i + 1)/2)
    = (1/6)*(i * (i + 1))((i*(i + 1)/2) + ((2*i + 1)/2) + 1/2)
    = (1/12) * (i * (i + 1)) * ((i*(i + 1)) + (2*i + 1) + 1)
    = (1/12) * (i * (i + 1)) * ((i2 + 3 * i + 2)
    = (1/12) * (i * (i + 1)) * ((i + 1) * (i + 2))
    = (1/12) * (i * (i + 1)2 * (i + 2))
Below is the implementation of this approach:  
C++
Java
Python3
C#
PHP
Javascript
// CPP Program to find the sum of sum of// squares of first n natural number#include <bits/stdc++.h>using namespace std; // Function to find sum of sum of square// of first n natural numberint findSum(int n){    return (n * (n + 1) * (n + 1) * (n + 2)) / 12;} // Driven Programint main(){    int n = 3;    cout << findSum(n) << endl;    return 0;}
// Java Program to find the sum of sum of// squares of first n natural numberclass GFG {     // Function to find sum of sum of    // square of first n natural number    static int findSum(int n)    {        return (n * (n + 1) *        (n + 1) * (n + 2)) / 12;    }         // Driver Program    public static void main(String[] args)    {        int n = 3;                 System.out.println(findSum(n) );    }} // This code is contributed by Arnab Kundu
# Python3 Program to find the sum# of sum of squares of first n# natural number # Function to find sum of sum of# square of first n natural numberdef findSum(n):    return ((n * (n + 1) * (n + 1)                 * (n + 2)) / 12) # Driven Programn = 3print(int(findSum(n))) # This code is contributed by# Prasad Kshirsagar
// C# Program to find the sum of sum of// squares of first n natural numberusing System; class GFG {     // Function to find sum of sum of    // square of first n natural number    static int findSum(int n)    {        return (n * (n + 1) * (n + 1)                     * (n + 2)) / 12;    }         // Driver Program    static public void Main()    {        int n = 3;                 Console.WriteLine(findSum(n) );    }} // This code is contributed by Arnab Kundu
<?php// PHP Program to find the sum of sum of// squares of first n natural number // Function to find sum of sum of square// of first n natural numberfunction findSum($n){    return ($n * ($n + 1) *           ($n + 1) * ($n +                   2)) / 12;}     // Driver Code    $n = 3;    echo findSum($n) ; // This code is contributed by nitin mittal?>
<script>// js Program to find the sum of sum of// squares of first n natural number // Function to find sum of sum of square// of first n natural numberfunction findSum($n){    return (n * (n + 1) *(n + 1) * (n + 2)) / 12;}     // Driver Code    n = 3;    document.write(findSum(n)) ; // This code is contributed by sravan kumar</script>
20
 
Time Complexity : O(1) Auxiliary Space : O(1) 
andrew1234
nitin mittal
Prasad_Kshirsagar
vt_m
mayanktyagi1709
sravankumar8128
kalrap615
series
series-sum
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Merge two sorted arrays with O(1) extra space
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Check if a number is Palindrome
Count ways to reach the n'th stair
Fizz Buzz Implementation
Median of two sorted arrays of same size
Product of Array except itself
Find Union and Intersection of two unsorted arrays