Average of Squares of Natural Numbers - GeeksforGeeks

24 Mar, 2021
Given a number n, find the average of square of natural Numbers till nExamples :  
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
C++
Java
Python3
C#
PHP
Javascript
// C program to calculate 1^2+2^2+3^2+... average// of square number#include <stdio.h> // Function to calculate  average of square numberfloat AvgofSquareN(int n){    float sum = 0;    for (int i = 1; i <= n; i++)        sum += (i * i);    return sum/n;} // Driver codeint main(){    int n = 2;    printf("%f", AvgofSquareN(n));    return 0;}
// C++ program to calculate 1^2+2^2+3^2+...// average of square number#include<bits/stdc++.h>using namespace std; // Function to calculate average of squaresfloat AvgofSquareN(int n){    float sum = 0;    for (int i = 1; i <= n; i++)        sum += (i * i);    return sum/n;} // Driver codeint main(){    int n = 2;    cout << AvgofSquareN(n) ;    return 0;}
// Java program to calculate 1^2+2^2+3^2+// ... average of square numberimport java.io.*; public class GFG{     // Function to calculate average// of square numberstatic float AvgofSquareN(int n){    float sum = 0;    for (int i = 1; i <= n; i++)        sum += (i * i);    return sum / n;} // Driver codestatic public void main (String[] args){    int n = 2;    System.out.println(AvgofSquareN(n)); }} // This code is contributed by vt_m.
# Python program to# calculate 1^2+2^2+3^2+...# average of square number # Function to calculate# average of square numberdef AvgofSquareN(n) :     sum = 0    for i in range(1, n + 1) :        sum += (i * i)    return sum/n # Driver coden = 2print (AvgofSquareN(n))     # This code is contributed by# Manish Shaw(manishshaw1)
// C# program to calculate 1^2+2^2+3^2+// ... average of square numberusing System; public class GFG{      // Function to calculate average// of square numberstatic float AvgofSquareN(int n){    float sum = 0;    for (int i = 1; i <= n; i++)        sum += (i * i);    return sum / n;} // Driver codestatic public void Main (String []args){    int n = 2;    Console.WriteLine(AvgofSquareN(n));     }} // This code is contributed by vt_m.
<?php// PHP program to calculate 1^2+2^2+3^2+...// average of square number // Function to calculate average// of square numberfunction AvgofSquareN( $n){    $sum = 0;    for ($i = 1; $i <= $n; $i++)        $sum += ($i * $i);    return $sum/$n;}     // Driver code    $n = 2;    echo(AvgofSquareN($n));     // This code is contributed by vt_m.?>
<script>// javascript program to calculate 1^2+2^2+3^2+... average// of square number // Function to calculate  average of square numberfunction AvgofSquareN( n){    let sum = 0;    for (let i = 1; i <= n; i++)        sum += (i * i);    return sum/n;} // Driver code    let n = 2;    document.write(AvgofSquareN(n).toFixed(6)); // This code is contributed by todaysgaurav </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
C++
Java
Python3
C#
PHP
Javascript
// C program to get the  Average of Square// of first n natural numbers#include <stdio.h> float AvgofSquareN(int n){    return (float)((n + 1) * (2 * n + 1)) / 6;} // Driver Codeint main(){    int n = 10;    printf("%f", AvgofSquareN(n));    return 0;}
// C++ program to get the  Average of Square// of first n natural numbers#include<bits/stdc++.h>using namespace std; float AvgofSquareN(int n){    return (float)((n + 1) * (2 * n + 1)) / 6;} // Driver Codeint main(){    int n = 10;    cout << AvgofSquareN(n) ;    return 0;}
// Java program to get the Average of// square of first n natural numbersimport java.io.*; public class GFG{ // Function to get the averagestatic float AvgofSquareN(int n){    return (float)((n + 1) * (2 *                    n + 1)) / 6;} // Driver Codestatic public void main (String[] args){    int n = 2;    System.out.println(AvgofSquareN(n)); }} // This code is contributed by vt_m. 
# PHP program to get the Average of# Square of first n natural numbers  def AvgofSquareN(n) :     return ((n + 1) * (2 * n + 1)) / 6; # Driver Coden = 2;print (AvgofSquareN(n));  # This code is contributed by Manish Shaw# (manishshaw1)
// C# program to get the Average of// squareof first n natural numbersusing System; public class GFG{ // Function to calculate averagestatic float AvgofSquareN(int n){    return (float)((n + 1) * (2 *                    n + 1)) / 6;} // Driver Codestatic public void Main (String []args){    int n = 2;    Console.WriteLine(AvgofSquareN(n));}} // This code is contributed by vt_m.
<?php// PHP program to get the Average of// Square of first n natural numbers function AvgofSquareN( $n){    return (($n + 1) * (2 * $n + 1)) / 6;}     // Driver Code    $n = 2;    echo(AvgofSquareN($n)); // This code is contributed by vt_m.?>
<script>// javascript program to get the Average of// square of first n natural numberspublic // Function to get the averagefunction AvgofSquareN(n){    return ((n + 1) * (2 *                    n + 1)) / 6;} // Driver Codevar n = 2;document.write(AvgofSquareN(n));  // This code is contributed by Amit Katiyar</script>
2.500000
Time Complexity: O(1)Space Complexity: O(1) 
vt_m
manishshaw1
himanshu77
todaysgaurav
amit143katiyar
series
series-sum
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Program to convert a given number to words
Modular multiplicative inverse
Count ways to reach the n'th stair
Program to multiply two matrices
Fizz Buzz Implementation
Program to print prime numbers from 1 to N.
Singular Value Decomposition (SVD)
Find first and last digits of a number
Check if a number is Palindrome