Posted on by Kalkicode
Code Mathematics

Average of cubes of first n natural numbers

Calculating the average of cubes of the first n natural numbers involves finding the sum of the cubes of the numbers from 1 to n and then dividing that sum by n. This is a simple mathematical problem that can be solved using a formula that leverages the properties of summation.

Problem Statement

Given a positive integer n, the problem is to find the average of the cubes of the first n natural numbers.

Idea to Solve the Problem

The sum of the cubes of the first n natural numbers can be represented by the sum of the cubes of consecutive integers, which is a well-known formula: 1³ + 2³ + 3³ + ... + n³. The formula for the sum of cubes is given by:

Sum = 1³ + 2³ + 3³ + ... + n³ = (n * (n + 1) / 2)²

The average of these cubes can be calculated by dividing the sum by n.

Pseudocode

function findAverageOfCube(n)
    if n <= 0
        return
    
    sum_of_cubes = (n * (n + 1) / 2)^2
    average = sum_of_cubes / n
    
    print average

Algorithm Explanation

  1. The findAverageOfCube function takes a positive integer n as input.

  2. If n is less than or equal to 0, the function returns immediately to handle invalid inputs.

  3. Calculate the sum of the cubes of the first n natural numbers using the formula (n * (n + 1) / 2)^2.

  4. Calculate the average by dividing the sum of cubes by n.

  5. Print the calculated average.

Program Solution

// C Program
// Average of cubes of first n natural numbers
#include <stdio.h>

void findAverageOfCube(int n)
{
	if (n <= 0)
	{
		return;
	}
	// Calculate cube of n natural number
	// Formula = (n×(n+1)²) / 4
	double average = (n * (n + 1) * (n + 1)) / 4.0;
	// Display calculated result
	printf("\n %lf", average);
}
int main()
{
	int n = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 

	*/
	findAverageOfCube(n);
	return 0;
}

Output

 73.500000
// Java program for
// Average of cubes of first n natural numbers
public class Average
{
	public void findAverageOfCube(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		double average = (n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		System.out.print("\n " + average);
	}
	public static void main(String[] args)
	{
		Average task = new Average();
		int n = 6;
		/*
		    n = 6
		    ---------------------------
		    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
		    ----------------------------
		    440
		    -- = 73.5
		     6 

		*/
		task.findAverageOfCube(n);
	}
}

Output

 73.5
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Average of cubes of first n natural numbers
class Average
{
	public: void findAverageOfCube(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		double average = (n *(n + 1) *(n + 1)) / 4.0;
		// Display calculated result
		cout << "\n " << average;
	}
};
int main()
{
	Average *task = new Average();
	int n = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	task->findAverageOfCube(n);
	return 0;
}

Output

 73.5
// Include namespace system
using System;
// Csharp program for
// Average of cubes of first n natural numbers
public class Average
{
	public void findAverageOfCube(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		double average = (n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		Console.Write("\n " + average);
	}
	public static void Main(String[] args)
	{
		Average task = new Average();
		int n = 6;
		/*
		    n = 6
		    ---------------------------
		    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
		    ----------------------------
		    440
		    -- = 73.5
		     6 
		*/
		task.findAverageOfCube(n);
	}
}

Output

 73.5
package main
import "fmt"
// Go program for
// Average of cubes of first n natural numbers
type Average struct {}
func getAverage() * Average {
	var me *Average = &Average {}
	return me
}
func(this Average) findAverageOfCube(n int) {
	if n <= 0 {
		return
	}
	// Calculate cube of n natural number
	// Formula = (n×(n+1)²) / 4
	var average float64 = float64(n * (n + 1) * (n + 1)) / 4.0
	// Display calculated result
	fmt.Print("\n ", average)
}
func main() {
	var task * Average = getAverage()
	var n int = 6
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	task.findAverageOfCube(n)
}

Output

 73.5
<?php
// Php program for
// Average of cubes of first n natural numbers
class Average
{
	public	function findAverageOfCube($n)
	{
		if ($n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		$average = ($n * ($n + 1) * ($n + 1)) / 4.0;
		// Display calculated result
		echo("\n ".$average);
	}
}

function main()
{
	$task = new Average();
	$n = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	$task->findAverageOfCube($n);
}
main();

Output

 73.5
// Node JS program for
// Average of cubes of first n natural numbers
class Average
{
	findAverageOfCube(n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		var average = (n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		process.stdout.write("\n " + average);
	}
}

function main()
{
	var task = new Average();
	var n = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	task.findAverageOfCube(n);
}
main();

Output

 73.5
#  Python 3 program for
#  Average of cubes of first n natural numbers
class Average :
	def findAverageOfCube(self, n) :
		if (n <= 0) :
			return
		
		#  Calculate cube of n natural number
		#  Formula = (n×(n+1)²) / 4
		average = (n * (n + 1) * (n + 1)) / 4.0
		#  Display calculated result
		print("\n ", average, end = "")
	

def main() :
	task = Average()
	n = 6
	#    n = 6
	#    ---------------------------
	#    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	#    ----------------------------
	#    440
	#    -- = 73.5
	#     6 
	task.findAverageOfCube(n)

if __name__ == "__main__": main()

Output

  73.5
#  Ruby program for
#  Average of cubes of first n natural numbers
class Average 
	def findAverageOfCube(n) 
		if (n <= 0) 
			return
		end

		#  Calculate cube of n natural number
		#  Formula = (n×(n+1)²) / 4
		average = (n * (n + 1) * (n + 1)) / 4.0
		#  Display calculated result
		print("\n ", average)
	end

end

def main() 
	task = Average.new()
	n = 6
	#    n = 6
	#    ---------------------------
	#    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	#    ----------------------------
	#    440
	#    -- = 73.5
	#     6 
	task.findAverageOfCube(n)
end

main()

Output

 73.5
// Scala program for
// Average of cubes of first n natural numbers
class Average()
{
	def findAverageOfCube(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		var average: Double = (n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		print("\n " + average);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Average = new Average();
		var n: Int = 6;
		/*
		    n = 6
		    ---------------------------
		    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
		    ----------------------------
		    440
		    -- = 73.5
		     6 
		*/
		task.findAverageOfCube(n);
	}
}

Output

 73.5
// Swift 4 program for
// Average of cubes of first n natural numbers
class Average
{
	func findAverageOfCube(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		let average: Double = Double(n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		print("\n ", average, terminator: "");
	}
}
func main()
{
	let task: Average = Average();
	let n: Int = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	task.findAverageOfCube(n);
}
main();

Output

  73.5
// Kotlin program for
// Average of cubes of first n natural numbers
class Average
{
	fun findAverageOfCube(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		val average: Double = (n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		print("\n " + average);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Average = Average();
	val n: Int = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	task.findAverageOfCube(n);
}

Output

 73.5

Time Complexity

The time complexity of this algorithm is constant (O(1)) because the calculations involve basic arithmetic operations that do not depend on 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.

New Comment