w3resource logo


php exercises

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

PHP Challenges - 1: Exercise-16 with Solution

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

Input : array(2, 6, 18, 54)
Output : Geometric sequence

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 a sequence of numbers is a geometric progression or not

Sample Solution :-

PHP Code :

<?php
function is_geometric($arr)
{
    if (sizeof($arr) <= 1)
        return True;
    # Calculate ratio
    $ratio = $arr[1]/$arr[0];
   
    # Check the ratio of the remaining
    for($i=1; $i<sizeof($arr); $i++)
    {
        if (($arr[$i]/($arr[$i-1])) != $ratio)
        {
            return "Not an geometric sequence";
        }
    }        
  return "Geometric  sequence";
}
$my_arr1 = array(2, 6, 18, 54);
$my_arr2 = array(10, 5, 2.5, 1.20);

print_r(is_geometric($my_arr1)."\n");
print_r(is_geometric($my_arr2)."\n");
?>

Output :

Geometric  sequence                                         
Not an geometric sequence

Flowchart:

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

PHP Code Editor:

<?php
function is_geometric($arr)
{
    if (sizeof($arr) <= 1)
        return True;
    # Calculate ratio
    $ratio = $arr[1]/$arr[0];
   
    # Check the ratio of the remaining
    for($i=1; $i<sizeof($arr); $i++)
    {
        if (($arr[$i]/($arr[$i-1])) != $ratio)
        {
            return "Not an geometric sequence";
        }
    }        
  return "Geometric  sequence";
}
$my_arr1 = array(2, 6, 18, 54);
$my_arr2 = array(10, 5, 2.5, 1.20);

print_r(is_geometric($my_arr1)."\n");
print_r(is_geometric($my_arr2)."\n");
?>

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.