# Functions and Algorithms Part B: Undoing Algorithms (20 minutes)

## Session 3, Part B

An algorithm is a recipe or a description of a mechanical set of steps for performing some task. For example, you can have an algorithm for making a peanut butter and jelly sandwich.

Mathematical algorithms are increasingly important in the computer age. Computer programs are essentially algorithms written in a language that computers understand. Here’s a mathematical algorithm (let’s call it Algorithm A):
Note 3

• Pick a number (that’s the input)
• Double it
• Divide that answer by 2
• Subtract 7 from what you get
• Multiply the result by 4 (that’s the output)

### Problem B1

Use Algorithm A for these problems.

1. If the input is 9, what is the output?
2. If the input is 10, what is the output?
3. If the input is n, what is the output?Tip: Try to run the algorithm’s steps using the variable n rather than a particular value. After the first step, the result is 2n. After the second step, the result is 2n + 2.
4. If the output is 28, what is the input?
5. If the output is 32, what is the input?
6. What input produces an output of 48?
7. What input produces an output of 36?

### Problem B2

What strategies did you use to answer parts (d)-(g) of Problem B1?

### Problem B3

Describe, in language similar to the way we described Algorithm A, an algorithm (call it Algorithm B) that undoes Algorithm A. This means that if you put a number into Algorithm A, then put that output into Algorithm B, you should end up with the original input.

### Problem B4

Does Algorithm A undo Algorithm B? That is, if you put a number into Algorithm B and then put that output into Algorithm A, do you get back to your starting number?

### Notes

Note 3

Discuss your responses to Problems B2 and B3. Then discuss the video segment of Dr. Fuji monitoring medication for a newborn. How is math being used at Boston Medical Center? Where else might the concepts of doing and undoing have real-world applications?

### Solutions

Problem B1

1. The output is 12.
2. The output is 16.
3. For an input n, the algorithm performs n >> 2n >> 2n + 2 >> n + 1 >> n – 6 >> 4n – 24.
4. Knowing that the output is 4n – 24 means that we have to solve 4n – 24 = 28, an equation that is solved by n = 13. This means that the input must be 13.
5. The input is 14.
6. The input is 18.
7. The input is 15.

Problem B2

Working backwards from the end of the algorithm to the beginning will undo it. There are other strategies, such as writing equations for each of (d)-(g).

Problem B3

Algorithm B “undoes” everything that Algorithm A does.

• Take the input (the output of Algorithm A) and
• Divide it by 4
• Double it
• Subtract 2
• Halve that
• There’s your output (of Algorithm B)

Problem B4

Test it out. See if you can figure out why it works by thinking of the algorithms as driving directions. Where would you end up if someone gave you directions like Algorithms A and B, and asked you to do them both? In short, yes, they undo each other.