A rabbit sits at the bottom of a staircase with n stairs.rabbit can hop up only one or two stairs at a time.how many different ways are there two reach at top of stair.
Suppose W(n) different ways are there two reach at top of stair.
Now W(1) = 1 [It can be done by a single step only] W(2) = 2 [It can be done by taking two stairs at one time or 2 steps of one stair]
W(3) = W(2) + W(1) [1st reach upto stair #1 and then move to stair #3 by taking double step or 1st reach upto stair #2 and then move to stair #3 by taking single step]
Given an integer n, write a function that returns count of trailing zeroes in n!. Examples: Input: n = 5 Output: 1 Factorial of 5 is 20 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24
Hint: use induction approach before reaching nth stair he can be either at n-1 or n-2 stairs.
ReplyDeleteSuppose W(n) different ways are there two reach at top of stair.
ReplyDeleteNow
W(1) = 1 [It can be done by a single step only]
W(2) = 2 [It can be done by taking two stairs at one time or 2 steps of one stair]
W(3) = W(2) + W(1) [1st reach upto stair #1 and then move to stair #3 by taking double step or
1st reach upto stair #2 and then move to stair #3 by taking single step]
Similarly W(n) = W(n-1) + W(n-2)
@Hsingh you are right.The sequence generated is like fibonacci series.
ReplyDelete