C++ Program for Sum of squares of first n natural numbers

16 Dec, 2021
Given a positive integer N. The task is to find 12 + 22 + 32 + ..... + N2.
Examples:
Input : N = 4
Output : 30
12 + 22 + 32 + 42
= 1 + 4 + 9 + 16
= 30

Input : N = 5
Output : 55

Method 1: O(N) The idea is to run a loop from 1 to n and for each i, 1 <= i <= n, find i2 to sum.
CPP
// CPP Program to find sum of square of first n natural numbers#include <bits/stdc++.h>using namespace std;  // Return the sum of the square // of first n natural numbersint squaresum(int n){    // Iterate i from 1 and n    // finding square of i and add to sum.    int sum = 0;    for (int i = 1; i <= n; i++)        sum += (i * i);    return sum;}  // Driven Programint main(){    int n = 4;    cout << squaresum(n) << endl;    return 0;}
30

Method 2: O(1)Proof:
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
We know,
(k + 1)3 = k3 + 3 * k2 + 3 * k + 1
We can write the above identity for k from 1 to n:
23 = 13 + 3 * 12 + 3 * 1 + 1 ......... (1)
33 = 23 + 3 * 22 + 3 * 2 + 1 ......... (2)
43 = 33 + 3 * 32 + 3 * 3 + 1 ......... (3)
53 = 43 + 3 * 42 + 3 * 4 + 1 ......... (4)
...
n3 = (n - 1)3 + 3 * (n - 1)2 + 3 * (n - 1) + 1 ......... (n - 1)
(n + 1)3 = n3 + 3 * n2 + 3 * n + 1 ......... (n)

Putting equation (n - 1) in equation n,
(n + 1)3 = (n - 1)3 + 3 * (n - 1)2 + 3 * (n - 1) + 1 + 3 * n2 + 3 * n + 1
         = (n - 1)3 + 3 * (n2 + (n - 1)2) + 3 * ( n + (n - 1) ) + 1 + 1

By putting all equation, we get
(n + 1)3 = 13 + 3 * Σ k2 + 3 * Σ k + Σ 1
n3 + 3 * n2 + 3 * n + 1 = 1 + 3 * Σ k2 + 3 * (n * (n + 1))/2 + n
n3 + 3 * n2 + 3 * n = 3 * Σ k2 + 3 * (n * (n + 1))/2 + n
n3 + 3 * n2 + 2 * n - 3 * (n * (n + 1))/2 = 3 * Σ k2
n * (n2 + 3 * n + 2) - 3 * (n * (n + 1))/2 = 3 * Σ k2
n * (n + 1) * (n + 2) - 3 * (n * (n + 1))/2 = 3 * Σ k2
n * (n + 1) * (n + 2 - 3/2) = 3 * Σ k2
n * (n + 1) * (2 * n + 1)/2  = 3 * Σ k2
n * (n + 1) * (2 * n + 1)/6  = Σ k2

CPP
// CPP Program to find sum// of square of first n// natural numbers#include <bits/stdc++.h>using namespace std;  // Return the sum of square of// first n natural numbersint squaresum(int n){    return (n * (n + 1) * (2 * n + 1)) / 6;}  // Driven Programint main(){    int n = 4;    cout << squaresum(n) << endl;    return 0;}
30

Avoiding early overflow:For large n, the value of (n * (n + 1) * (2 * n + 1)) would overflow. We can avoid overflow up to some extent using the fact that n*(n+1) must be divisible by 2.
CPP
// CPP Program to find sum of square of first// n natural numbers. This program avoids// overflow upto some extent for large value// of n.#include <bits/stdc++.h>using namespace std;  // Return the sum of square of first n natural// numbersint squaresum(int n){    return (n * (n + 1) / 2) * (2 * n + 1) / 3;}  // Driven Programint main(){    int n = 4;    cout << squaresum(n) << endl;    return 0;}
30

Please refer complete article on Sum of squares of first n natural numbers for more details!
varshagumber28
C++ Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Const keyword in C++
cout in C++
Program to implement Singly Linked List in C++ using class
Different ways to print elements of vector
Dynamic _Cast in C++
string::npos in C++ with Examples
Why it is important to write "using namespace std" in C++ program?
How to convert a Vector to Set in C++
Maximum value of long long int in C++