Average of Squares of Natural Numbers
Given a number n, find the average of square of natural Numbers till n
Examples :
Input : n = 2
Output :2.5
12 + 22 = 2.5
Input : n = 3
Output : 4.666667
12 + 22 + 32 = 4.666667
Naive approach :
A naive approach will be to run a loop from 1 to n and and find the average of sum up all the squares.
C
#include <stdio.h>
float AvgofSquareN( int n)
{
float sum = 0;
for ( int i = 1; i <= n; i++)
sum += (i * i);
return sum/n;
}
int main()
{
int n = 2;
printf ( "%f" , AvgofSquareN(n));
return 0;
}
|
C++
#include<bits/stdc++.h>
using namespace std;
float AvgofSquareN( int n)
{
float sum = 0;
for ( int i = 1; i <= n; i++)
sum += (i * i);
return sum/n;
}
int main()
{
int n = 2;
cout << AvgofSquareN(n) ;
return 0;
}
|
Java
import java.io.*;
public class GFG{
static float AvgofSquareN( int n)
{
float sum = 0 ;
for ( int i = 1 ; i <= n; i++)
sum += (i * i);
return sum / n;
}
static public void main (String[] args)
{
int n = 2 ;
System.out.println(AvgofSquareN(n));
}
}
|
Python3
def AvgofSquareN(n) :
sum = 0
for i in range ( 1 , n + 1 ) :
sum + = (i * i)
return sum / n
n = 2
print (AvgofSquareN(n))
|
C#
using System;
public class GFG{
static float AvgofSquareN( int n)
{
float sum = 0;
for ( int i = 1; i <= n; i++)
sum += (i * i);
return sum / n;
}
static public void Main (String []args)
{
int n = 2;
Console.WriteLine(AvgofSquareN(n));
}
}
|
PHP
<?php
function AvgofSquareN( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
$sum += ( $i * $i );
return $sum / $n ;
}
$n = 2;
echo (AvgofSquareN( $n ));
?>
|
Javascript
<script>
function AvgofSquareN( n)
{
let sum = 0;
for (let i = 1; i <= n; i++)
sum += (i * i);
return sum/n;
}
let n = 2;
document.write(AvgofSquareN(n).toFixed(6));
</script>
|
Output :
2.500000
Time Complexity: O(n)
Space Complexity: O(1)
Efficient Approach : Sum of squares of natural numbers (n + 1)(2n + 1) / 6. Therefore average is n(n + 1)(2n + 1) / 6 * n = (n + 1)(2n + 1) / 6.
C
#include <stdio.h>
float AvgofSquareN( int n)
{
return ( float )((n + 1) * (2 * n + 1)) / 6;
}
int main()
{
int n = 10;
printf ( "%f" , AvgofSquareN(n));
return 0;
}
|
C++
#include<bits/stdc++.h>
using namespace std;
float AvgofSquareN( int n)
{
return ( float )((n + 1) * (2 * n + 1)) / 6;
}
int main()
{
int n = 10;
cout << AvgofSquareN(n) ;
return 0;
}
|
Java
import java.io.*;
public class GFG{
static float AvgofSquareN( int n)
{
return ( float )((n + 1 ) * ( 2 *
n + 1 )) / 6 ;
}
static public void main (String[] args)
{
int n = 2 ;
System.out.println(AvgofSquareN(n));
}
}
|
Python3
def AvgofSquareN(n) :
return ((n + 1 ) * ( 2 * n + 1 )) / 6 ;
n = 2 ;
print (AvgofSquareN(n));
|
C#
using System;
public class GFG{
static float AvgofSquareN( int n)
{
return ( float )((n + 1) * (2 *
n + 1)) / 6;
}
static public void Main (String []args)
{
int n = 2;
Console.WriteLine(AvgofSquareN(n));
}
}
|
PHP
<?php
function AvgofSquareN( $n )
{
return (( $n + 1) * (2 * $n + 1)) / 6;
}
$n = 2;
echo (AvgofSquareN( $n ));
?>
|
Javascript
<script>
function AvgofSquareN(n)
{
return ((n + 1) * (2 *
n + 1)) / 6;
}
var n = 2;
document.write(AvgofSquareN(n));
</script>
|
2.500000
Time Complexity: O(1)
Space Complexity: O(1)
Using the reduce() function from functools in python:
Approach:
Import the functools module.
Define the function avg_squares that takes an integer argument n.
Use the reduce() function from the functools module to iterate over the range of numbers from 1 to n, calculating the sum of squares of each number in the sequence.
Divide the sum by n to get the average of the squares of natural numbers.
Return the average.
Example usage to test the function with different inputs.
Python3
import functools
def avg_squares(n):
sum = functools. reduce ( lambda x, y: x + y * y, range ( 1 ,n + 1 ), 0 )
return sum / n
print (avg_squares( 2 ))
print (avg_squares( 3 ))
|
Output
2.5
4.666666666666667
Time Complexity: O(n)
Space Complexity: O(1)
Last Updated :
14 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...