Using High School Algebra Problems to Learn Algorithmic Thinking

I was reading the morning paper just now and came across the following puzzle:

A watermelon vendor sells half of her melons, and then restocks 200 more at the end of first day. On the next day, she sells half of what she has; on the third day, she sells 30 melons and counts her remaining melons to be 600. How many melons did she have at the beginnning?

If one knows algebra, then this is just an exercise in mathematical modelling and solving a simple linear equation with one unknown. In some countries, this kind of problems are even taught at the senior primary level. Unfortunately, forcing young students at such an age to learn a fairly abstract concept like algebra make back-fire – the youngster may very well end up hating math for the rest of his or her life because of the unpleasantness of the experience.

There is another way that can potentially help students solve this problem in a way that is experimental and more fun. Why not start with a guess of the answer (any will do)? Say, 4000? If this is true, then at the end of first day, the vendor should have 2200 melons; the second day, 1100; and the third, 1070. Oops, it is not 600, so the guess is an overestimate. Well, no worries, we can adjust the initial guess downwards to say, 2000. This should give 1200 melons on the first day; 600 on the second, and 570 on the third, close to 600 but slightly underestimated. OK, so this tells us that the answer is something a little more than 2000. If you continue this train of thought you should be able to discover the solution easily.

The alternative method that we have just looked at is an example of problem solving by algorithmic thinking. This approach essentially involves iterating a same sequence of steps until the solution is found, usually because some stopping criterion has been fulfilled (e.g. melon count on day 3 is 600 in the above example). The fun part is one can often use particular features of the problem to improve the algorithm. For example, since melons are sold as a whole (in general), the answer must be a multiple of 20, since otherwise, the count on day 2 cannot be an integer. For example, if we guess 2110, then at the end of the first day we have 1255 melons, but there is no way to sell half of 1255 melons if one has to sell them whole! So we could actually revise our guess upwards or downwards in steps of 20.

Of course, doing this by hand is tedious, so we ask the computer to do it for us. Here’s an implementation of the algorithm in R.

#Start with a guess
x <- 4000

#Initialize remainder
remainder <- x

while(TRUE){

#Break-out from loop when solution is found
if(remainder == 0){print(x) ; break}

x1 <- x/2 + 200
x2 <- x1/2
remainder <- (x2 – 30) – 600

if(remainder > 0) {x <- x-20}
if(remainder < 0) {x <- x+20}

}

About Tsung Fei

A teacher, researcher in the bioinformatics division at the University of Malaya
This entry was posted in Education, Fun. Bookmark the permalink.

9 Responses to Using High School Algebra Problems to Learn Algorithmic Thinking

1. Sophos says:

Probably many youngsters will end up hating maths. But i think geniuses will love maths even more instead of finding it boring and then get turned off.

If analytical solution is easy, I will opt for that. If analytical solution is difficult and/or requires some calculus or some complex algebra (e.g. logarithm on one side and exponential on the other), algorithm is the way!

Of course it’s still best to combine both. Sometimes writing an algorithm can be a very tough job (for me at least).

• Tsung Fei says:

Yes, we need to know both approaches. They complement each other and widen the set of tools available to us for attacking a problem.

2. jlkuan says:

Senior primary level? Gee, I feel so incompetent haha. Took me 2 mins to understand the puzzle and longer time than I should to solve it by hand.

About the algorithm with multiple of 20, can it be other multiples like 40 perhaps? and how did the multiple of 20 come about?

• Tsung Fei says:

This kind of questions are asked in math competitions at the primary level. However, such competitions have an artificial feel to it sometimes … a student is often not taught how to think through the problem (takes a long time), but merely shown lots of examples and sample solutions. The danger is they tend to develop a “use this/that formula” approach to deal with problems, which is fine for standardised problems but hardly useful in unfamiliar situations.

The multiple of 20 rule GUARANTEES that the count on day 2 is divisible by 2. No such guarantees can be given with multiples of other types. 40 , by chance, works in this case (4000), but not 60, or 80 (the looping continues forever).

• Sophos says:

By the way, I think you are wrong. jlkuan would be right regarding multiples of 40. I’ll give a counter example.

Let’s say I have 20 (multiple of 20 but not 40) watermelons on the first day. I sold half, 10. I add 200, 210. The next day I sell half again. Woops, 105. We will need the last digit to be zero. Hence multiple of 40 is more accurate.

And thus, multiple of 80 works as well. Since 80 is a multiple of 40. Just that we may miss the answer since we’re skipping some number.

• Tsung Fei says:

You have brought up an important point … it appears that a particular step size may or may not work, depending on the different starting guesses. It appears that a step size that works all the time regardless of starting guess is simply 1!

3. Tsung Fei says:

By the way, copying the R codes from a wordpress site and running them will give errors. Apparently, some hidden stuff is copied as well and one can’t see them.

4. jlkuan says:

Yes. I got the error of “unexpected input” when I run the copied codes from wordpress site using R. It was solved by retyping the codes using R text editor. It seems that wordpress site has characters incompatibility with some R versions (http://support.rstudio.org/help/discussions/problems/386-error-unexpected-input-in).

5. jin yung says:

#I prefer to set while remainder not equal to 600. just different style.
while(remainder!=600){
x1 <- x/2 + 200
x2 <- x1/2
remainder 600) {x <- x-20}
if(remainder < 600) {x <- x+20}
}
print(x)