Program for Sum of squares of first n natural numbers
Last Updated : 29 Mar, 2025
Given a positive integer n, we have to find the sum of squares of first n natural numbers.
Examples :
Input : n = 2
Output: 5
Explanation: 1^2+2^2 = 5
Input : n = 8
Output: 204
Explanation : 1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 = 204
[Naive Approach] - Adding One By One - O(n) Time and O(1) Space
The idea for this naive approach is to run a loop from 1 to n and sum up all the squares.
C++ #include <iostream> using namespace std; int summation(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum += (i * i); return sum; } int main() { int n = 2; cout << summation(n); return 0; }
C #include <stdio.h> int summation(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum += (i * i); return sum; } int main() { int n = 2; printf("%d",summation(n)); return 0; }
Java import java.util.*; import java.lang.*; class GfG { public static int summation(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum += (i * i); return sum; } public static void main(String args[]) { int n = 2; System.out.println(summation(n)); } }
Python def summation(n): return sum([i**2 for i in range(1, n + 1)]) if __name__ == "__main__": n = 2 print(summation(n))
C# using System; class GfG { public static int summation(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum += (i * i); return sum; } public static void Main() { int n = 2; Console.WriteLine(summation(n)); } }
JavaScript function summation(n) { let sum = 0; for (let i = 1; i <= n; i++) sum += (i * i); return sum; } let n = 2; console.log(summation(n));
The idea for this approach is to use the mathematical formulae for the sum of squares of first n natural numbers.
12 + 22 + ......... + n2 = n(n+1)(2n+1) / 6
We can prove this formula using induction. We can easily see that the formula is true for n = 1 and n = 2 as sums are 1 and 5 respectively.
Let it be true for n = k-1. So sum of k-1 numbers
is (k – 1) * k * (2 * k – 1)) / 6
In the following steps, we show that it is true
for k assuming that it is true for k-1.
Sum of k numbers = Sum of k-1 numbers + k2
= (k – 1) * k * (2 * k – 1) / 6 + k2
= ((k2 – k) * (2*k – 1) + 6k2)/6
= (2k3 – 2k2 – k2 + k + 6k2)/6
= (2k3 + 3k2 + k)/6
= k * (k + 1) * (2*k + 1) / 6
Example : Find sum of squares of the first 3 natural numbers
Solution:
= 3 * (3 + 1) * (2*3 + 1) / 6
= (3 * 4 * 7) / 6
= 84 / 6
= 14
C++ #include <iostream> using namespace std; int summation(int n) { return (n * (n + 1) * (2 * n + 1)) / 6; } int main() { int n = 10; cout << summation(n) << endl; return 0; }
C #include <stdio.h> int summation(int n) { return (n * (n + 1) * (2 * n + 1)) / 6; } int main() { int n = 10; printf("%d", summation(n)); return 0; }
Java import java.util.*; import java.lang.*; class GFG { public static int summation(int n) { return (n * (n + 1) * (2 * n + 1)) / 6; } public static void main(String args[]) { int n = 10; System.out.println(summation(n)); } }
Python # Python code to find sum of # squares of first n natural numbers. def summation(n): return (n * (n + 1) * (2 * n + 1)) / 6 # Driver Code if __name__ == '__main__': n = 10 print(summation(n))
C# using System; class GFG { public static int summation(int n) { return (n * (n + 1) * (2 * n + 1)) / 6; } public static void Main() { int n = 10; Console.WriteLine(summation(n)); } }
JavaScript function summation(n) { return (n * (n + 1) * (2 * n + 1)) / 6; } let n = 10; console.log(summation(n));
Avoiding the overflow:
In the above method, sometimes due to large value of n, the value of (n * (n + 1) * (2 * n + 1)) would overflow. We can avoid this overflow up to some extent using the fact that n*(n+1) must be divisible by 2 and restructuring the formula as (n * (n + 1) / 2) * (2 * n + 1) / 3;
C++ #include <iostream> using namespace std; int summation(int n) { //to avoid overflow //n*(n + 1)*(2 * n + 1) / 6 = (n * (n + 1) / 2) * (2 * n + 1) / 3; return (n * (n + 1) / 2) * (2 * n + 1) / 3; } int main() { int n = 10; cout << summation(n) << endl; return 0; }
Java import java.io.*; class GFG { static int summation(int n) { //to avoid overflow //n*(n + 1)*(2 * n + 1) / 6 = (n * (n + 1) / 2) * (2 * n + 1) / 3; return (n * (n + 1) / 2) * (2 * n + 1) / 3; } public static void main (String[] args) { int n = 10; System.out.println(summation(n)); } }
Python def summation(n): #to avoid overflow #n*(n + 1)*(2 * n + 1) / 6 = (n * (n + 1) / 2) * (2 * n + 1) / 3; return (n * (n + 1) // 2) * (2 * n + 1) // 3 def main(): n = 10 print(summation(n)) if __name__ == "__main__": main()
C# using System; class MainClass { public static int Summation(int n) { //to avoid overflow //n*(n + 1)*(2 * n + 1) / 6 = (n * (n + 1) / 2) * (2 * n + 1) / 3; return (n * (n + 1) / 2) * (2 * n + 1) / 3; } public static void Main(string[] args) { int n = 10; Console.WriteLine(Summation(n)); } }
JavaScript function summation(n) { //to avoid overflow //n*(n + 1)*(2 * n + 1) / 6 = (n * (n + 1) / 2) * (2 * n + 1) / 3; return (n * (n + 1) / 2) * (2 * n + 1) / 3; } n = 10; console.log(summation(n));