There are n stairs, and a person standing at the bottom can climb either 1 stair or 2 stairs at a time, count the number of ways that a person can reach at the top.
This algorithm calculates the number of distinct ways to climb a staircase with 'n' steps.
def climb(n):
if n < 1:
return 0
if n == 1:
return 1
if n == 2:
return 2
return climb(n - 1) + climb(n - 2)
n = 5
print(climb(n))
function climb(n) {
if (n < 1) {
return 0;
}
if (n === 1) {
return 1;
}
if (n === 2) {
return 2;
}
return climb(n - 1) + climb(n - 2);
}
const n = 5;
console.log(climb(n));