Difference between sum of the squares of first n natural numbers and square of sum
Last Updated :
28 Mar, 2023
Given an integer n, find the absolute difference between sum of the squares of first n natural numbers and square of sum of first n natural numbers.
Examples :
Input : n = 3
Output : 22
Sum of first three numbers is 3 + 2 + 1 = 6
Square of the sum = 36
Sum of squares of first three is 9 + 4 + 1 = 14
Absolute difference = 36 - 14 = 22
Input : n = 10
Output : 2640
Asked in : biwhiz Company
Brute Force:
Iterate over the first n natural numbers twice: once to calculate the sum of their squares, and once to calculate their sum. Then, the absolute difference between the sum of squares and the square of the sum can be computed using the formula:
(1^2 + 2^2 + … + n^2) – (1 + 2 + … + n)^2
C++
#include<bits/stdc++.h>
using namespace std;
int Square_Diff( int n){
int sumOfSquares = 0;
int sum = 0;
for ( int i = 1; i <= n; i++) {
sumOfSquares += pow (i, 2);
sum += i;
}
int squareOfSum = pow (sum, 2);
int absDifference = abs (sumOfSquares - squareOfSum);
return absDifference;
}
int main() {
int n = 10;
cout << Square_Diff(n);
return 0;
return 0;
}
|
Java
import java.lang.Math;
public class Main {
public static int squareDiff( int n) {
int sumOfSquares = 0 ;
int sum = 0 ;
for ( int i = 1 ; i <= n; i++) {
sumOfSquares += Math.pow(i, 2 );
sum += i;
}
int squareOfSum = ( int ) Math.pow(sum, 2 );
int absDifference = Math.abs(sumOfSquares - squareOfSum);
return absDifference;
}
public static void main(String[] args) {
int n = 10 ;
System.out.println(squareDiff(n));
}
}
|
Python3
import math
def square_diff(n):
sum_of_squares = 0
sum_ = 0
for i in range ( 1 , n + 1 ):
sum_of_squares + = math. pow (i, 2 )
sum_ + = i
square_of_sum = math. pow (sum_, 2 )
abs_difference = abs (sum_of_squares - square_of_sum)
return int (abs_difference)
n = 10
print (square_diff(n))
|
C#
using System;
namespace SquareDifference {
class Program {
static int Square_Diff( int n)
{
int sumOfSquares = 0;
int sum = 0;
for ( int i = 1; i <= n; i++) {
sumOfSquares += ( int )Math.Pow(
i, 2);
sum += i;
}
int squareOfSum = ( int )Math.Pow(
sum, 2);
int absDifference = Math.Abs(
sumOfSquares
- squareOfSum);
return absDifference;
}
static void Main( string [] args)
{
int n = 10;
Console.WriteLine(Square_Diff(
n));
}
}
}
|
Javascript
function Square_Diff(n) {
let sumOfSquares = 0;
let sum = 0;
for (let i = 1; i <= n; i++) {
sumOfSquares += Math.pow(i, 2);
sum += i;
}
let squareOfSum = Math.pow(sum, 2);
let absDifference = Math.abs(sumOfSquares - squareOfSum);
return absDifference;
}
const n = 10;
console.log(Square_Diff(n));
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach :
1. Find the sum of square of first n natural numbers.
2. Find the sum of first n numbers and square it.
3. Find the absolute difference between both the sums and print it.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int Square_Diff( int n){
int l, k, m;
l = (n * (n + 1) * (2 * n + 1)) / 6;
k = (n * (n + 1)) / 2;
k = k * k;
m = abs (l - k);
return m;
}
int main()
{
int n = 10;
cout << Square_Diff(n);
return 0;
}
|
Java
public class GfG{
static int Square_Diff( int n){
int l, k, m;
l = (n * (n + 1 ) * ( 2 * n + 1 )) / 6 ;
k = (n * (n + 1 )) / 2 ;
k = k * k;
m = Math.abs(l - k);
return m;
}
public static void main(String s[])
{
int n = 10 ;
System.out.println(Square_Diff(n));
}
}
|
Python
def Square_Diff(n):
l = (n * (n + 1 ) * ( 2 * n + 1 )) / 6
k = (n * (n + 1 )) / 2
k = k * * 2
m = abs (l - k)
return int (m)
print (Square_Diff( 10 ))
|
C#
using System;
public class GFG
{
static int Square_Diff( int n)
{
int l, k, m;
l = (n * (n + 1) * (2 * n + 1)) / 6;
k = (n * (n + 1)) / 2;
k = k * k;
m = Math.Abs(l - k);
return m;
}
public static void Main()
{
int n = 10;
Console.WriteLine(Square_Diff(n));
}
}
|
PHP
<?php
function Square_Diff( $n )
{
$l ;
$k ;
$m ;
$l = ( $n * ( $n + 1) *
(2 * $n + 1)) / 6;
$k = ( $n * ( $n + 1)) / 2;
$k = $k * $k ;
$m = abs ( $l - $k );
return $m ;
}
$n = 10;
echo Square_Diff( $n );
?>
|
Javascript
<script>
function Square_Diff(n){
var l, k, m;
l = (n * (n + 1) * (2 * n + 1)) / 6;
k = (n * (n + 1)) / 2;
k = k * k;
m = Math.abs(l - k);
return m;
}
var n = 10;
document.write(Square_Diff(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...