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 then add them all.
We can find sum of squares of first n natural number by formula: n * (n + 1)* (2*n + 1)/6
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++)
sum += ((i * (i + 1) * (2 * i + 1)) / 6);
return sum;
}
int main()
{
int n = 3;
cout << findSum(n) << endl;
return 0;
}
|
Java
class GFG {
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;
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println( findSum(n));
}
}
|
Python3
def findSum(n):
summ = 0
for i in range ( 1 , n + 1 ):
summ = (summ + ((i * (i + 1 )
* ( 2 * i + 1 )) / 6 ))
return summ
n = 3
print ( int (findSum(n)))
|
C#
using System;
public class GFG {
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;
}
static public void Main()
{
int n = 3;
Console.WriteLine(findSum(n));
}
}
|
PHP
<?php
function findSum( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
$sum += (( $i * ( $i + 1) *
(2 * $i + 1)) / 6);
return $sum ;
}
$n = 3;
echo findSum( $n ) ;
?>
|
Javascript
<script>
function findSum(n)
{
return (n * (n + 1) * (n + 1) * (n + 2)) / 12;
}
let n = 3;
document.write(findSum(n) + "<br>" );
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int findSum( int n)
{
return (n * (n + 1) * (n + 1) * (n + 2)) / 12;
}
int main()
{
int n = 3;
cout << findSum(n) << endl;
return 0;
}
|
Java
class GFG {
static int findSum( int n)
{
return (n * (n + 1 ) *
(n + 1 ) * (n + 2 )) / 12 ;
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println(findSum(n) );
}
}
|
Python3
def findSum(n):
return ((n * (n + 1 ) * (n + 1 )
* (n + 2 )) / 12 )
n = 3
print ( int (findSum(n)))
|
C#
using System;
class GFG {
static int findSum( int n)
{
return (n * (n + 1) * (n + 1)
* (n + 2)) / 12;
}
static public void Main()
{
int n = 3;
Console.WriteLine(findSum(n) );
}
}
|
PHP
<?php
function findSum( $n )
{
return ( $n * ( $n + 1) *
( $n + 1) * ( $n +
2)) / 12;
}
$n = 3;
echo findSum( $n ) ;
?>
|
Javascript
<script>
function findSum($n)
{
return (n * (n + 1) *(n + 1) * (n + 2)) / 12;
}
n = 3;
document.write(findSum(n)) ;
</script>
|
Time Complexity : O(1)
Auxiliary Space : O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!