w3resource

PHP Challenges: Check whether a sequence of numbers is a geometric progression or not

PHP Challenges - 1: Exercise-16 with Solution

Write a PHP program to check whether a sequence of numbers is a geometric progression or not.

Input : array(2, 6, 18, 54)

In mathematics, a geometric progression or geometric sequence, is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. For example, the sequence 2, 6, 18, 54, ... is a geometric progression with common ratio 3. Similarly, 10, 5, 2.5, 1.25, ... is a geometric sequence with common ratio 1/2.

Explanation :

PHP: Check whether a sequence of numbers is a geometric progression or not

Sample Solution :

PHP Code :

<?php
// Function to check if an array forms a geometric sequence
function is_geometric($arr)
{
    // If the array has 1 or fewer elements, it's automatically considered a geometric sequence
    if (sizeof($arr) <= 1)
        return True;
    
    // Calculate the ratio between the second and first elements of the array
    $ratio = $arr[1] / $arr[0];
   
    // Check the ratio of the remaining elements
    for ($i = 1; $i < sizeof($arr); $i++)
    {
        // If the ratio of any pair of consecutive elements is not equal to the initial ratio, return "Not a geometric sequence"
        if (($arr[$i] / ($arr[$i - 1])) != $ratio)
        {
            return "Not a geometric sequence";
        }
    }        

    // If all ratios are equal, return "Geometric sequence"
    return "Geometric sequence";
}

// Define two arrays
$my_arr1 = array(2, 6, 18, 54);
$my_arr2 = array(10, 5, 2.5, 1.20);

// Test the is_geometric function with the defined arrays and print the results
print_r(is_geometric($my_arr1) . "\n");
print_r(is_geometric($my_arr2) . "\n");
?>

Explanation:

Here is a brief explanation of the above PHP code:

  • Function definition:
    • The code defines a function named "is_geometric()" which checks if an array forms a geometric sequence. It takes an array '$arr' as input.
  • Inside the function:
    • It first checks if the size of the array is 1 or less. If so, it returns 'True' as any sequence with 1 or fewer elements is considered geometric.
    • It calculates the ratio between the second and first elements of the array.
    • It iterates through the array elements starting from the second element.
    • Within each iteration, it checks if the ratio of the current element to the previous element is equal to the initial ratio calculated.
    • If any ratio is not equal to the initial ratio, it returns "Not a geometric sequence".
    • If all ratios are equal, it returns "Geometric sequence".
  • Function Call & Testing:
    • Two arrays '$my_arr1' and '$my_arr2' are defined with different sets of numbers.
    • The "is_geometric()" function is called twice with these arrays as input.
    • Finally the results of the function calls are printed using "print_r()", which displays whether each input array forms a geometric sequence.

Sample Output:

Geometric  sequence                                         
Not a geometric sequence

Flowchart:

PHP Flowchart: Check whether a sequence of numbers is a geometric progression or not

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to check whether a sequence of numbers is an arithmetic progression or not.
Next: Write a PHP program to compute the sum of the two reversed numbers and display the sum in reversed form.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/challenges/1/php-challenges-1-exercise-16.php