Open In App

PHP Program for Sum of Squares of First N Natural Numbers

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Calculating the sum of squares of the first n natural numbers is a common problem in programming, demonstrating the application of mathematical formulas, loops, and PHP functions. This article explores various methods to solve this problem in PHP, providing insights into basic arithmetic operations, iterative solutions, and direct formula application.

Understanding the Problem

The task is to find the sum of squares of the first n natural numbers. Mathematically, it is represented as the sum S = 1^2 + 2^2 + 3^2 + … + n^2. This problem has both iterative and formula-based solutions, offering a good opportunity to explore different programming techniques.

Approach 1: Using a Loop

The simplest way to approach this problem is by using a loop to iterate through the first n natural numbers, squaring each number and adding it to a sum variable.

PHP




<?php
  
function sumOfSquares($n) {
    $sum = 0;
    for ($i = 1; $i <= $n; $i++) {
        $sum += $i * $i;
    }
    return $sum;
}
  
// Driver code
$n = 5;
echo "Sum of squares of first $n natural numbers is: " 
    . sumOfSquares($n);
  
?>


Output

Sum of squares of first 5 natural numbers is: 55

Approach 2: Using the Formula

A more efficient way to solve this problem, especially for large values of n, is by using a direct mathematical formula: S = n(n + 1)(2n + 1) / 6.

PHP




<?php
  
function sumOfSquares($n) {
    return ($n * ($n + 1) * (2 * $n + 1)) / 6;
}
  
// Driver code
$n = 5;
echo "Sum of squares of first $n natural numbers is: " 
    . sumOfSquares($n);
  
?>


Output

Sum of squares of first 5 natural numbers is: 55

Approach 3: Using Array Functions

PHP offers powerful array functions that can be used creatively to solve this problem. This approach involves creating an array of the first n natural numbers, applying a square operation to each element, and then summing up the results.

PHP




<?php
  
function sumOfSquares($n) {
    $numbers = range(1, $n);
    $squaredNumbers = array_map(function($num) {
        return $num * $num;
    }, $numbers);
    return array_sum($squaredNumbers);
}
  
// Driver code
$n = 5;
echo "Sum of squares of first $n natural numbers is: " 
    . sumOfSquares($n);
  
?>


Output

Sum of squares of first 5 natural numbers is: 55

Approach 4: Recursive Solution

Although not the most efficient for this particular problem, a recursive solution demonstrates an important programming concept. This method involves solving the problem by breaking it down into smaller instances of itself.

PHP




<?php
  
function sumOfSquares($n) {
    if ($n == 1) {
        return 1; // Base case
    } else {
        return $n * $n + sumOfSquares($n - 1);
    }
}
  
// Driver code
$n = 5;
echo "Sum of squares of first $n natural numbers is: " 
    . sumOfSquares($n);
  
?>


Output

Sum of squares of first 5 natural numbers is: 55


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads