PHP Program for Count ways to reach the n\’th stair

There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top.

stairs

Consider the example shown in diagram. The value of n is 3. There are 3 ways to reach the top. The diagram is taken from Easier Fibonacci puzzles

PHP



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A PHP program to count 
// number of ways to reach 
// n'th stair when a person
// can climb 1, 2, ..m stairs 
// at a time. 
  
// A simple recursive
// function to find n'th 
// fibonacci number
function fib($n)
{
if ($n <= 1)
    return $n;
return fib($n - 1) + 
       fib($n - 2);
}
  
// Returns number of 
// ways to reach s'th stair
function countWays($s)
{
    return fib($s + 1);
}
  
// Driver Code
$s = 4;
echo "Number of ways = "
           countWays($s);
  
// This code is contributed
// by m_kit
?>

chevron_right


Output:

Number of ways = 5

The time complexity of the above implementation is exponential (golden ratio raised to power n). It can be optimized to work in O(Logn) time using the previously discussed Fibonacci function optimizations.

PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A PHP program to count 
// number of ways to reach 
// n'th stair when a person 
// can climb either 1 or 2 
// stairs at a time 
  
// A recursive function
// used by countWays
function countWaysUtil($n, $m)
{
    if ($n <= 1)
        return $n;
    $res = 0;
    for ($i = 1; $i <= $m && 
                 $i <= $n; $i++)
        $res += countWaysUtil($n - $i, $m);
    return $res;
}
  
// Returns number of ways
// to reach s'th stair
function countWays($s, $m)
{
    return countWaysUtil($s + 1, $m);
}
  
// Driver Code
$s = 4; $m = 2;
echo "Nuber of ways = ",
      countWays($s, $m);
      
// This code is contributed
// by akt_mit
?>

chevron_right


Output:

Nuber of ways = 5

PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A PHP program to count number 
// of ways to reach n't stair when
// a person can climb 1, 2, ..m 
// stairs at a time
  
// A recursive function used by countWays
function countWaysUtil($n, $m)
{
    $res[0] = 1; $res[1] = 1;
    for ($i = 2; $i < $n; $i++)
    {
        $res[$i] = 0;
        for ($j = 1; $j <= $m && $j <= $i; $j++)
        $res[$i] += $res[$i - $j];
    }
    return $res[$n - 1];
}
  
// Returns number of ways
// to reach s'th stair
function countWays($s, $m)
{
    return countWaysUtil($s + 1, $m);
}
  
    // Driver Code
    $s = 4; 
    $m = 2;
    echo "Nuber of ways = ", countWays($s, $m);
  
// This code is contributed by m_kit 
?>

chevron_right


Output:

Nuber of ways = 5

Please refer complete article on Count ways to reach the n’th stair for more details!



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.