The Grasshopper 🦗 and the Lily Pads 🪷

A Dynamic Programming Problem Visualization

Let's explore a coding challenge from the website www.codewars.com. The challenge is called Prime Grasshopper.

Imagine you are a grasshopper. Ahead of you are lily pads in a straight line. You can jump to any of the pads to start with. After that you are allowed to jump to another pad as long as it is a prime number of pads further down the line. That is, if you are on pad 1, you can jump 2, 3, 5, 7, etc. to pad number 3, 4, 6, 8, etc.

You can only jump to the right, to a high numbered pad. Each pad has seeds on it, and your goal is to maximize the total number of seeds gathered from your journey. Oh, and a pad can have a negative number of seeds. So much for reality.

Here's an example: we have 5 pads numbered 0 to 4 (because computer programming languages like to start numbering at 0 for some reason) and the number of seeds, in order, is 2, 3, 4, 5, 6.

Figure 1: Some weird looking 🪷

We are interactive! You can click on the lily pads. When you do, the seeds from that pad are added to your total. But don't forget the rule! The pads have to be a prime number apart. We can't let you break the rules. :-)

If your lily pads are not a prime number apart, you will get the message "Path is not valid!" and the pads will be colored red.

The best total for Figure 1 is 12, can you get it?

What do you think a good algorithm would be to find the best path for any set of lily pads?

Figure 2: Where to go?

Here's another one, can you solve it?

Hint: The largest number is not guaranteed to be a good landing.

You're doing great! Let's try another! Click the button when ready.

Try another easy one. Click here.