- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find number of ways we can reach to the next floor using stairs in Python

Suppose there is a stair case with N steps. One can either go step by step, or at each step, one can take a jump of at most N steps. We have to find the number of ways in which we can go to the top floor. The N value may be large we are only interested in the first and the last K digits of number of ways.

So, if the input is like N = 10 k = 2, then the output will be 63 because there are 10 steps, if there are S number of ways in which we can go to the top, then consider S be of the form wxyz.So, summing wx + yz is 63.

To solve this, we will follow these steps −

- N := N - 1
- c := 2 * ceiling of (k + log(N);base10)
- e := N, b := 2, s := 1
- while e > 0, do
- if e is odd, then
- s := the first p-c digit numbers of (s*b) where p is number of digits in s*b

- e := floor of e/2
- b := the first p-c digit numbers of (b*b) where p is number of digits in b*b

- if e is odd, then
- s := first p - k digit number of s, where p is number of digits in s
- r := s + (2^N) mod 10^k
- return r

## Example

Let us see the following implementation to get better understanding −

from math import log10,ceil def solve(N,k): N -= 1 c = 2*ceil(k + log10(N)) e = N b = 2 s = 1 while e > 0: if e % 2 == 1: s = int(str(s*b)[:c]) e //=2 b = int(str(b*b)[:c]) s = str(s)[:k] r = int(s) + pow(2, N, 10**k) return r N = 10 k = 2 print(solve(N,k))

## Input

10, 2

## Output

63

- Related Articles
- Program to find how many ways we can climb stairs in Python
- Program to find number of ways we can reach from top left point to bottom right point in Python
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to find number of ways we can get n R.s using Indian denominations in Python
- Program to find number of ways we can arrange symbols to get target in Python?
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Program to count number of ways we can distribute coins to workers in Python
- Program to count number of ways we can throw n dices in Python
- Program to find maximum number of coins we can get using Python
- Program to find minimum number of vertices to reach all nodes using Python
- Program to find number of ways we can get a number which is sum of nth power of unique numbers in Python
- Program to find number of good ways to split a string using Python

Advertisements