Find the Sum of Cubes of First N Natural Numbers

problem-solving
Published

September 27, 2024

Finding the sum of the cubes of the first n natural numbers is a common problem in mathematics and programming. While you could use a simple loop to iterate and sum, a more efficient approach leverages mathematical formulas for a faster solution, especially when dealing with large values of n. This post explores both methods in Python, highlighting the advantages of the mathematical approach.

The Brute-Force (Iterative) Method

The most straightforward way to solve this problem is using a loop. We iterate through the numbers from 1 to n, cube each number, and accumulate the sum. Here’s the Python code:

def sum_of_cubes_iterative(n):
  """Calculates the sum of cubes of first n natural numbers iteratively.

  Args:
    n: The number of natural numbers to consider.

  Returns:
    The sum of the cubes of the first n natural numbers.  Returns 0 if n is 0 or negative.
  """
  if n <= 0:
    return 0
  total = 0
  for i in range(1, n + 1):
    total += i**3
  return total

n = 5
result = sum_of_cubes_iterative(n)
print(f"The sum of cubes of first {n} natural numbers (iterative): {result}")  # Output: 225

This method is easy to understand but becomes computationally expensive for large values of n.

The Mathematical Formula Method

A significantly more efficient approach involves using the mathematical formula for the sum of cubes:

∑ᵢ₌₁ⁿ i³ = (n(n+1)/2)²

This formula directly calculates the sum without the need for iteration, making it much faster for larger inputs. Here’s the Python code:

def sum_of_cubes_formula(n):
  """Calculates the sum of cubes of first n natural numbers using the formula.

  Args:
    n: The number of natural numbers to consider.

  Returns:
    The sum of the cubes of the first n natural numbers. Returns 0 if n is 0 or negative.
  """
  if n <= 0:
    return 0
  return (n * (n + 1) // 2)**2

n = 5
result = sum_of_cubes_formula(n)
print(f"The sum of cubes of first {n} natural numbers (formula): {result}")  # Output: 225

n = 1000
result = sum_of_cubes_formula(n)
print(f"The sum of cubes of first {n} natural numbers (formula): {result}") # Output: 250500250000

This method offers a considerable performance advantage, especially when dealing with a large number of natural numbers. The use of // ensures integer division, preventing potential floating-point inaccuracies.

Comparing the Two Methods

The iterative approach is simpler to understand but suffers from linear time complexity (O(n)). The formula-based approach, however, exhibits constant time complexity (O(1)), making it far superior for larger values of n. For small values of n, the difference might be negligible, but as n grows, the efficiency of the mathematical formula becomes strikingly apparent. Choosing the appropriate method depends on the specific needs of your application and the expected size of n.