Count ways to reach the nth stair using step 1 , 2 or 3 | A recursive function used by countWays ; declaring three variables and holding the ways for first three stairs ; d = 0 fourth variable ; starting from 4 as already counted for 3 stairs ; Driver program to test above functions
def countWays ( n ) : NEW_LINE INDENT a = 1 NEW_LINE b = 2 NEW_LINE c = 4 NEW_LINE if ( n == 0 or n == 1 or n == 2 ) : NEW_LINE INDENT return n NEW_LINE DEDENT if ( n == 3 ) : NEW_LINE INDENT return c NEW_LINE DEDENT for i in range ( 4 , n + 1 ) : NEW_LINE INDENT d = c + b + a NEW_LINE a = b NEW_LINE b = c NEW_LINE c = d NEW_LINE DEDENT return d NEW_LINE DEDENT n = 4 NEW_LINE print ( countWays ( n ) ) NEW_LINE