Average of squares of natural numbers
The problem entails calculating the average of the squares of the first n natural numbers. Natural numbers are the positive integers starting from 1. The task is to find the average value of the squares of these natural numbers.
Problem Statement
Given a positive integer n, the goal is to calculate the average 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 average of these squares is (1 + 4 + 9 + 16) / 4 = 30 / 4 = 7.5.
Idea to Solve
To solve this problem, one can utilize the sum formula for the squares of consecutive natural numbers. The formula is
given by: 1^2 + 2^2 + 3^2 + ... + n^2 = (n * (n + 1) * (2 * n + 1)) / 6
. Then, the average can be
calculated by dividing the sum of squares by n.
Pseudocode
function averageOfNaturalNo(n)
sum = (n * (n + 1) * (2 * n + 1)) / 6
average = sum / n
print "Average of squares of", n, "natural numbers is:", average
Algorithm Explanation
- Calculate the sum of squares of the first n natural numbers using the formula
sum = (n * (n + 1) * (2 * n + 1)) / 6
. - Calculate the average by dividing the sum by n:
average = sum / n
. - Print the result.
Code Solution
/*
C program for
Average of squares of natural numbers
*/
#include <stdio.h>
// Average of series of squares of natural numbers
void averageOfNaturalNo(int n)
{
long double sum = 0.0;
double result = 0.0;
if (n > 0)
{
// Calculate sum of squares of n natural numbers
sum = ((n *(n + 1) *(2 *n + 1)) / 6);
// Calculate average
result = sum / n;
}
// Display calculated result
printf(" Average of squares of %d natural number is : %lf \n",
n, result);
}
int main(int argc, char
const *argv[])
{
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)² = 55
55
-- = 11
5
*/
averageOfNaturalNo(5);
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)² = 204
204
---- = 25.5
8
*/
averageOfNaturalNo(8);
return 0;
}
Output
Average of squares of 5 natural number is : 11.000000
Average of squares of 8 natural number is : 25.500000
/*
Java program for
Average of squares of natural numbers
*/
public class NaturalNumbers
{
// Average of series of squares of natural numbers
public void averageOfNaturalNo(int n)
{
double sum = 0.0;
double result = 0.0;
if (n > 0)
{
// Calculate sum of squares of n natural numbers
sum = ((n * (n + 1) * (2 * n + 1)) / 6);
// Calculate average
result = sum / n;
}
// Display calculated result
System.out.print(" Average of squares of " + n + " natural number is : " + result + " \n");
}
public static void main(String[] args)
{
NaturalNumbers task = new NaturalNumbers();
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)² = 55
55
-- = 11
5
*/
task.averageOfNaturalNo(5);
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)² = 204
204
---- = 25.5
8
*/
task.averageOfNaturalNo(8);
}
}
Output
Average of squares of 5 natural number is : 11.0
Average of squares of 8 natural number is : 25.5
// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Average of squares of natural numbers
*/
class NaturalNumbers
{
public:
// Average of series of squares of natural numbers
void averageOfNaturalNo(int n)
{
double sum = 0.0;
double result = 0.0;
if (n > 0)
{
// Calculate sum of squares of n natural numbers
sum = ((n *(n + 1) *(2 *n + 1)) / 6);
// Calculate average
result = sum / n;
}
// Display calculated result
cout << " Average of squares of "
<< n << " natural number is : "
<< result << " \n";
}
};
int main()
{
NaturalNumbers *task = new NaturalNumbers();
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)² = 55
55
-- = 11
5
*/
task->averageOfNaturalNo(5);
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)² = 204
204
---- = 25.5
8
*/
task->averageOfNaturalNo(8);
return 0;
}
Output
Average of squares of 5 natural number is : 11
Average of squares of 8 natural number is : 25.5
// Include namespace system
using System;
/*
Csharp program for
Average of squares of natural numbers
*/
public class NaturalNumbers
{
// Average of series of squares of natural numbers
public void averageOfNaturalNo(int n)
{
double sum = 0.0;
double result = 0.0;
if (n > 0)
{
// Calculate sum of squares of n natural numbers
sum = ((n * (n + 1) * (2 * n + 1)) / 6);
// Calculate average
result = sum / n;
}
// Display calculated result
Console.Write(" Average of squares of " +
n + " natural number is : " +
result + " \n");
}
public static void Main(String[] args)
{
NaturalNumbers task = new NaturalNumbers();
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)² = 55
55
-- = 11
5
*/
task.averageOfNaturalNo(5);
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)² = 204
204
---- = 25.5
8
*/
task.averageOfNaturalNo(8);
}
}
Output
Average of squares of 5 natural number is : 11
Average of squares of 8 natural number is : 25.5
package main
import "fmt"
/*
Go program for
Average of squares of natural numbers
*/
// Average of series of squares of natural numbers
func averageOfNaturalNo(n int) {
var sum float64 = 0.0
var result float64 = 0.0
if n > 0 {
// Calculate sum of squares of n natural numbers
sum = float64((n * (n + 1) * (2 * n + 1)) / 6.0)
// Calculate average
result = sum / float64(n)
}
// Display calculated result
fmt.Print(" Average of squares of ",
n, " natural number is : ", result, " \n")
}
func main() {
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)² = 55
55
-- = 11
5
*/
averageOfNaturalNo(5)
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)² = 204
204
---- = 25.5
8
*/
averageOfNaturalNo(8)
}
Output
Average of squares of 5 natural number is : 11.0
Average of squares of 8 natural number is : 25.5
<?php
/*
Php program for
Average of squares of natural numbers
*/
class NaturalNumbers
{
// Average of series of squares of natural numbers
public function averageOfNaturalNo($n)
{
$sum = 0.0;
$result = 0.0;
if ($n > 0)
{
// Calculate sum of squares of n natural numbers
$sum = ((($n * ($n + 1) * (2 * $n + 1)) / 6));
// Calculate average
$result = $sum / $n;
}
// Display calculated result
echo(" Average of squares of ".$n.
" natural number is : ".$result.
" \n");
}
}
function main()
{
$task = new NaturalNumbers();
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)² = 55
55
-- = 11
5
*/
$task->averageOfNaturalNo(5);
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)² = 204
204
---- = 25.5
8
*/
$task->averageOfNaturalNo(8);
}
main();
Output
Average of squares of 5 natural number is : 11
Average of squares of 8 natural number is : 25.5
/*
Node JS program for
Average of squares of natural numbers
*/
class NaturalNumbers
{
// Average of series of squares of natural numbers
averageOfNaturalNo(n)
{
var sum = 0.0;
var result = 0.0;
if (n > 0)
{
// Calculate sum of squares of n natural numbers
sum = (((n * (n + 1) * (2 * n + 1)) / 6));
// Calculate average
result = sum / n;
}
// Display calculated result
process.stdout.write(" Average of squares of " +
n + " natural number is : " +
result + " \n");
}
}
function main()
{
var task = new NaturalNumbers();
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)² = 55
55
-- = 11
5
*/
task.averageOfNaturalNo(5);
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)² = 204
204
---- = 25.5
8
*/
task.averageOfNaturalNo(8);
}
main();
Output
Average of squares of 5 natural number is : 11
Average of squares of 8 natural number is : 25.5
# Python 3 program for
# Average of squares of natural numbers
class NaturalNumbers :
# Average of series of squares of natural numbers
def averageOfNaturalNo(self, n) :
sum = 0.0
result = 0.0
if (n > 0) :
# Calculate sum of squares of n natural numbers
sum = (((n * (n + 1) * (2 * n + 1)) / 6))
# Calculate average
result = sum / n
# Display calculated result
print(" Average of squares of ",
n ," natural number is : ",
result ," ")
def main() :
task = NaturalNumbers()
# Test A
# n = 5
# (1)² + (2)² + (3)² + (4)² + (5)² = 55
# 55
# -- = 11
# 5
task.averageOfNaturalNo(5)
# Test B
# n = 8
# (1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
# (7)² + (8)² = 204
# 204
# ---- = 25.5
# 8
task.averageOfNaturalNo(8)
if __name__ == "__main__": main()
Output
Average of squares of 5 natural number is : 11.0
Average of squares of 8 natural number is : 25.5
# Ruby program for
# Average of squares of natural numbers
class NaturalNumbers
# Average of series of squares of natural numbers
def averageOfNaturalNo(n)
sum = 0.0
result = 0.0
if (n > 0)
# Calculate sum of squares of n natural numbers
sum = ((n * (n + 1) * (2 * n + 1)) / 6.0)
# Calculate average
result = sum / n
end
# Display calculated result
print(" Average of squares of ",
n ," natural number is : ", result ," \n")
end
end
def main()
task = NaturalNumbers.new()
# Test A
# n = 5
# (1)² + (2)² + (3)² + (4)² + (5)² = 55
# 55
# -- = 11
# 5
task.averageOfNaturalNo(5)
# Test B
# n = 8
# (1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
# (7)² + (8)² = 204
# 204
# ---- = 25.5
# 8
task.averageOfNaturalNo(8)
end
main()
Output
Average of squares of 5 natural number is : 11.0
Average of squares of 8 natural number is : 25.5
/*
Scala program for
Average of squares of natural numbers
*/
class NaturalNumbers()
{
// Average of series of squares of natural numbers
def averageOfNaturalNo(n: Int): Unit = {
var sum: Double = 0.0;
var result: Double = 0.0;
if (n > 0)
{
// Calculate sum of squares of n natural numbers
sum = ((n * (n + 1) * (2 * n + 1)) / 6);
// Calculate average
result = sum / n;
}
// Display calculated result
print(" Average of squares of " +
n + " natural number is : " + result + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: NaturalNumbers = new NaturalNumbers();
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)² = 55
55
-- = 11
5
*/
task.averageOfNaturalNo(5);
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)² = 204
204
---- = 25.5
8
*/
task.averageOfNaturalNo(8);
}
}
Output
Average of squares of 5 natural number is : 11.0
Average of squares of 8 natural number is : 25.5
/*
Swift 4 program for
Average of squares of natural numbers
*/
class NaturalNumbers
{
// Average of series of squares of natural numbers
func averageOfNaturalNo(_ n: Int)
{
var sum: Double = 0.0;
var result: Double = 0.0;
if (n > 0)
{
// Calculate sum of squares of n natural numbers
sum = Double((n * (n + 1) * (2 * n + 1)) / 6);
// Calculate average
result = sum / Double(n);
}
// Display calculated result
print(" Average of squares of",
n ,"natural number is : ", result ," ");
}
}
func main()
{
let task: NaturalNumbers = NaturalNumbers();
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)² = 55
55
-- = 11
5
*/
task.averageOfNaturalNo(5);
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)² = 204
204
---- = 25.5
8
*/
task.averageOfNaturalNo(8);
}
main();
Output
Average of squares of 5 natural number is : 11.0
Average of squares of 8 natural number is : 25.5
/*
Kotlin program for
Average of squares of natural numbers
*/
class NaturalNumbers
{
// Average of series of squares of natural numbers
fun averageOfNaturalNo(n: Int): Unit
{
var sum: Double ;
var result: Double = 0.0;
if (n > 0)
{
// Calculate sum of squares of n natural numbers
sum = ((n * (n + 1) * (2 * n + 1)) / 6.0);
// Calculate average
result = sum / n;
}
// Display calculated result
print(" Average of squares of " +
n + " natural number is : " +
result + " \n");
}
}
fun main(args: Array < String > ): Unit
{
val task: NaturalNumbers = NaturalNumbers();
/*
Test A
n = 5
(1)² + (2)² + (3)² + (4)² + (5)² = 55
55
-- = 11
5
*/
task.averageOfNaturalNo(5);
/*
Test B
n = 8
(1)² + (2)² + (3)² + (4)² + (5)² + (6)² +
(7)² + (8)² = 204
204
---- = 25.5
8
*/
task.averageOfNaturalNo(8);
}
Output
Average of squares of 5 natural number is : 11.0
Average of squares of 8 natural number is : 25.5
Time Complexity
The time complexity of this algorithm is constant, O(1), as it involves only a few mathematical operations that do not depend on the input size. The space complexity is also constant, as it uses a fixed number of variables.
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.
New Comment