A frog needs to jump across 20 lily pads. He starts on the shore (Number 0) and ends precisely on the last lily pad (Number 20). He can jump one or two lily pads at a time. How many different ways can he reach his destination?
What if he can jump one, two or three at a time? Or four? Five? Six? Etc.
Solution
1. The frog can jump one or two pads at a time.
There is just one way of traversing zero lily pads (stay on the shore), and just one of traversing a single pad: jump to it.
For any other number nn of pads, the number of ways of traversing nn pads is simply the sum of the numbers of ways of traversing n−1n−1 and n−2n−2 pads. That’s because every way of traversing nn pads either starts with a jump to the first pad and is completed by a way of traversing n−1n−1 more pads, or starts with a jump to the second pad and is completed by a way of traversing n−2n−2 more.
So the sequence, for nn from 00 to 2020, of the numbers of ways of traversing nn pads, is the start (or the start after the first element, if the first element is 00) of the Fibonacci Sequence: 1,1,2,3,5,8,…,109461,1,2,3,5,8,…,10946.
We can calculate that in 1919 simple two-integer sums, or we can use the following closed-form expression (if you find exponentiation to be an improvement over the very simple recurrence, which at least in this case is questionable):
1√5[(1+√52)21−(1−√52)21]1√5⎡⎣(1+√52)21−(1−√52)21⎤⎦2. The frog can jump from one to m pads at a time.
Let Wm(n)Wm(n) be the number of ways of traversing nn lily pads, given that the frog can jump up to mm at a time.
For any mm, Wm(0)=1Wm(0)=1 and for n>0, Wm(n) is the sum of Wm(i), where i ranges over the up-to-m positive integers that precede n:
Wm(n)=n−1∑i=max(0,n−m)Wm(i)This yields a generalization of the Fibonacci Sequence, known as the n-Step Fibonacci Sequence. The values of Wm(20) are listed below. As a sanity-check, we can verify that W20(20) is 219, as it should be, because that’s how many possible choices of stopping-pads there are on the way to the last lily pad.
Code (Python)
N = 20
W = {}
for m in range(1,N+1):
W[(0,m)] = 1
for n in range(1,N+1):
for m in range (1,N+1):
W[(n,m)] = 0
for i in range(max(0,n-m),n):
W[(n,m)] += W[(i,m)]
print "Max Jump, Ways:"
for m in range(1,N+1):
print m,",", W[(N,m)]
Output:
Max Jump, Ways:
1 , 1
2 , 10946
3 , 121415
4 , 283953
5 , 400096
6 , 463968
7 , 495776
8 , 510994
9 , 518145
10 , 521472
11 , 523008
12 , 523712
13 , 524032
14 , 524176
15 , 524240
16 , 524268
17 , 524280
18 , 524285
19 , 524287
20 , 524288
[Finished in 0.1s]