Posted on by Kalkicode
Code Mathematics

# Sum of squares of first n natural numbers

The problem at hand involves calculating the sum of the squares of the first n natural numbers. Natural numbers are the positive integers starting from 1. The task is to find the sum of the squares of these natural numbers.

## Problem Statement

Given a positive integer n, the objective is to calculate the sum of the squares of the first n natural numbers.

## Example

For n = 4, the first n natural numbers are 1, 2, 3, and 4. The squares of these numbers are 1, 4, 9, and 16. The sum of these squares is 1 + 4 + 9 + 16 = 30.

## Idea to Solve

To solve this problem, you can either directly calculate the sum using a loop or use a mathematical formula for the sum of squares. The sum of squares of the first n natural numbers can be calculated using the formula: `sum = n * (n + 1) * (2n + 1) / 6`.

## Pseudocode

``````function squaresOfNaturalNo(n)
sum = n * (n + 1) * (2 * n + 1) / 6
print "Sum of square in", n, "natural numbers is", sum``````

## Algorithm Explanation

1. Calculate the sum of the squares of the first n natural numbers using the formula `sum = n * (n + 1) * (2 * n + 1) / 6`.
2. Print the result.

## Code Solution

``````/*
C program for
Sum of squares of first n natural numbers
*/
#include <stdio.h>

void squaresOfNaturalNo(int n)
{
long long int sum = 0;
if (n > 0)
{
// Normal calculation
// Execute loop 1 to n and calculate squared.
for (int i = 1; i <= n; ++i)
{
sum += (i *i);
}
}
printf("\n Sum of square in %d natural number is %lld", n, sum);
}
int main(int argc, char const *argv[])
{
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)²  = 55
*/
squaresOfNaturalNo(5);
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)²  = 204
*/
squaresOfNaturalNo(8);
return 0;
}``````

#### Output

`````` Sum of square in 5 natural number is 55
Sum of square in 8 natural number is 204``````
``````/*
Java program for
Sum of squares of first n natural numbers
*/
public class NaturalNumbers
{
public void squaresOfNaturalNo(int n)
{
long sum = 0;
if (n > 0)
{
// Normal calculation
// Execute loop 1 to n and calculate squared.
for (int i = 1; i <= n; ++i)
{
sum += (i * i);
}
}
System.out.print("\n Sum of square in " +
n + " natural number is " + sum);
}
public static void main(String[] args)
{
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)²  = 55

*/
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)²  = 204
*/
}
}``````

#### Output

`````` Sum of square in 5 natural number is 55
Sum of square in 8 natural number is 204``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program for
Sum of squares of first n natural numbers
*/
class NaturalNumbers
{
public: void squaresOfNaturalNo(int n)
{
long sum = 0;
if (n > 0)
{
// Normal calculation
// Execute loop 1 to n and calculate squared.
for (int i = 1; i <= n; ++i)
{
sum += (i *i);
}
}
cout << "\n Sum of square in " << n
<< " natural number is " << sum;
}
};
int main()
{
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)²  = 55

*/
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)²  = 204
*/
return 0;
}``````

#### Output

`````` Sum of square in 5 natural number is 55
Sum of square in 8 natural number is 204``````
``````// Include namespace system
using System;
/*
Csharp program for
Sum of squares of first n natural numbers
*/
public class NaturalNumbers
{
public void squaresOfNaturalNo(int n)
{
long sum = 0;
if (n > 0)
{
// Normal calculation
// Execute loop 1 to n and calculate squared.
for (int i = 1; i <= n; ++i)
{
sum += (i * i);
}
}
Console.Write("\n Sum of square in " + n +
" natural number is " + sum);
}
public static void Main(String[] args)
{
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)²  = 55

*/
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)²  = 204
*/
}
}``````

#### Output

`````` Sum of square in 5 natural number is 55
Sum of square in 8 natural number is 204``````
``````package main
import "fmt"
/*
Go program for
Sum of squares of first n natural numbers
*/

func squaresOfNaturalNo(n int) {
var sum int64 = 0
if n > 0 {
// Normal calculation
// Execute loop 1 to n and calculate squared.
for i := 1 ; i <= n ; i++ {
sum += int64(i * i)
}
}
fmt.Print("\n Sum of square in ", n,
" natural number is ", sum)
}
func main() {

/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)²  = 55

*/
squaresOfNaturalNo(5)
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)²  = 204
*/
squaresOfNaturalNo(8)
}``````

#### Output

`````` Sum of square in 5 natural number is 55
Sum of square in 8 natural number is 204``````
``````<?php
/*
Php program for
Sum of squares of first n natural numbers
*/
class NaturalNumbers
{
public	function squaresOfNaturalNo(\$n)
{
\$sum = 0;
if (\$n > 0)
{
// Normal calculation
// Execute loop 1 to n and calculate squared.
for (\$i = 1; \$i <= \$n; ++\$i)
{
\$sum += (\$i * \$i);
}
}
echo("\n Sum of square in ".\$n.
" natural number is ".\$sum);
}
}

function main()
{
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)²  = 55

*/
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)²  = 204
*/
}
main();``````

#### Output

`````` Sum of square in 5 natural number is 55
Sum of square in 8 natural number is 204``````
``````/*
Node JS program for
Sum of squares of first n natural numbers
*/
class NaturalNumbers
{
squaresOfNaturalNo(n)
{
var sum = 0;
if (n > 0)
{
// Normal calculation
// Execute loop 1 to n and calculate squared.
for (var i = 1; i <= n; ++i)
{
sum += (i * i);
}
}
process.stdout.write("\n Sum of square in " + n +
" natural number is " + sum);
}
}

function main()
{
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)²  = 55

*/
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)²  = 204
*/
}
main();``````

#### Output

`````` Sum of square in 5 natural number is 55
Sum of square in 8 natural number is 204``````
``````#    Python 3 program for
#    Sum of squares of first n natural numbers
class NaturalNumbers :
def squaresOfNaturalNo(self, n) :
sum = 0
if (n > 0) :
i = 1
#  Normal calculation
#  Execute loop 1 to n and calculate squared.
while (i <= n) :
sum += (i * i)
i += 1

print("\n Sum of square in",
n ,"natural number is ",
sum, end = "")

def main() :
#   Test A
#   n = 5
#   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
#   Test B
#   n = 8
#   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
#   (7)² + (8)²  = 204

if __name__ == "__main__": main()``````

#### Output

`````` Sum of square in 5 natural number is  55
Sum of square in 8 natural number is  204``````
``````#    Ruby program for
#    Sum of squares of first n natural numbers
class NaturalNumbers
def squaresOfNaturalNo(n)
sum = 0
if (n > 0)
i = 1
#  Normal calculation
#  Execute loop 1 to n and calculate squared.
while (i <= n)
sum += (i * i)
i += 1
end

end

print("\n Sum of square in ",
n ," natural number is ", sum)
end

end

def main()
#   Test A
#   n = 5
#   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
#   Test B
#   n = 8
#   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
#   (7)² + (8)²  = 204
end

main()``````

#### Output

`````` Sum of square in 5 natural number is 55
Sum of square in 8 natural number is 204``````
``````/*
Scala program for
Sum of squares of first n natural numbers
*/
class NaturalNumbers()
{
def squaresOfNaturalNo(n: Int): Unit = {
var sum: Long = 0;
if (n > 0)
{
var i: Int = 1;
// Normal calculation
// Execute loop 1 to n and calculate squared.
while (i <= n)
{
sum += (i * i);
i += 1;
}
}
print("\n Sum of square in " + n +
" natural number is " + sum);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: NaturalNumbers = new NaturalNumbers();
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)²  = 55

*/
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)²  = 204
*/
}
}``````

#### Output

`````` Sum of square in 5 natural number is 55
Sum of square in 8 natural number is 204``````
``````/*
Swift 4 program for
Sum of squares of first n natural numbers
*/
class NaturalNumbers
{
func squaresOfNaturalNo(_ n: Int)
{
var sum: Int = 0;
if (n > 0)
{
var i: Int = 1;
// Normal calculation
// Execute loop 1 to n and calculate squared.
while (i <= n)
{
sum += (i * i);
i += 1;
}
}
print("\n Sum of square in", n ,
"natural number is ", sum, terminator: "");
}
}
func main()
{
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)²  = 55

*/
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)²  = 204
*/
}
main();``````

#### Output

`````` Sum of square in 5 natural number is  55
Sum of square in 8 natural number is  204``````
``````/*
Kotlin program for
Sum of squares of first n natural numbers
*/
class NaturalNumbers
{
fun squaresOfNaturalNo(n: Int): Unit
{
var sum: Long = 0;
if (n > 0)
{
var i: Int = 1;
// Normal calculation
// Execute loop 1 to n and calculate squared.
while (i <= n)
{
sum += (i * i);
i += 1;
}
}
print("\n Sum of square in " + n +
" natural number is " + sum);
}
}
fun main(args: Array < String > ): Unit
{
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)²  = 55

*/
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)²  = 204
*/
}``````

#### Output

`````` Sum of square in 5 natural number is 55
Sum of square in 8 natural number is 204``````

## Time Complexity

The time complexity of this algorithm is O(n), as it involves a loop that iterates from 1 to n to calculate the sum. The space complexity is constant, as it uses a fixed number of variables regardless of the input size.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post