beanz Magazine

Samantha the Frog

US Department of Agriculture

Here's a fun math problem you can work out with pen and paper as well as Python.

Samantha the frog found herself at the bottom of a 30 foot deep well. Starting at the bottom, each day she jumps up three feet and remains in place until sun sets. Each night while sleeping, she slides back down two feet. How many days will it take Samantha to escape from the well?

Well, on day one she jumps up to the three meter mark. At night one she slides back down to the one meter mark. On day two she jumps from one meter up to the four meter mark, and then slides back down to the two meter mark as she sleeps. And this keeps happening over and over until and at some point we most likely lose track of, or miscalculate, the day, the night, or Samantha’s height.

However, if we make a quick sketch and start listing where the frog is each morning, maybe we can find a pattern to help out.

Morning Start Evening End
1 0 ft 3 ft 1 ft
2 1 ft 4 ft 2 ft
3 2 ft 5 ft 3 ft
4 3 ft 6 ft 4 ft
26 25 ft 28 ft 26 ft
27 26 ft 39 ft 27 ft
28 27 ft 30 ft and OUT!

By following a pattern, we see that on day 28 Samantha jumps to the 30 foot mark. We are going to assume that once she reaches the top of the well she is out and hops out, so she will not slide back down two feet that evening.

However, based on the wording of the question, it could be argued only her front legs grip the outside of the wall when she reaches 30 feet, and that she will remain there to slide back down two feet as she sleeps that evening. In that case, it is not until day 29 that she jumps from a height of 28 feet, up and over the edge to freedom.

The question leaves room for discussion. Either way, we could write up a few lines of code to follow Samantha through her journey to the top of the well:

Pseudocode

startPoint = 0

While the frog is less than 30 feet up, repeat the following process:

  • Add 3 to the startPoint
  • Subtract 2 from the result
  • Reset the starting point to this number

Python Code

#frog starts on the ground, 0 feet up
start = 0
#we need to create an ending point variable before we can reference it. We could have set it to any value
day1End = 0
#we’ll be keeping track of the days that pass, so we need a variable to store this in from the get go
day = 1
while(day1End < 30):
    day1End = start + 3
    day2Start = day1End - 2
    start = day2Start
    print(“day:” ,day, “evening location:”, day1End, “feet”)
    day = day + 1

This code is written in Python, but could be easily rewritten in any programming language, or even easily animated in a drag and drop environment such as Scratch. The online version of this article describes how to create then solve this puzzle with Scratch. You also can type the Python code into the http://repl.it website and play around with different parts of the code.

For example, try adjusting the code for a well that is 100 feet deep or a frog that jumps up 5 feet instead of 3. With less than 5 keystrokes, the code can be adjusted to solve a whole array of related questions!

Learn More







Also In The June 2017 Issue

Can we measure the time and steps required for things to happen?

This Canadian experiment used a robot to explore how people respond to robots and technology.

An amazing new book turns math problems into shapes and illustrations.

This pen and paper project helps organize ideas into stories with a finite state machine.

While you can't use soap and water on your code, you can keep your code as sparkly clean as any dish or silverware.

This project explores the basics of using Google's Static Map software to display your own maps.

Most people love cookies. But these cookies are the kind that make the internet possible.

Links from the bottom of all the June 2017 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for June 2017.

Software languages don't magically appear. They're created by design. First in a series.