Writing and performing algorithms

During the past week, I’ve spent a lot of time trying to look through this website and trying to improve it.  I am hoping to provide the most convenient and useful experience as possible.  While WordPress has done a wonderful job providing the structure needed to set up a very good website without coding, I still wanted to customize the website further.  In order to do so, I did indeed need to go through code and make small changes.  Even though this wasn’t an extensive project, it was still fun to go through and have to problem solve and code for the website.  During this process, I was reminded again of the daily interaction we have with technology.  Furthermore, even though most of this works without a second thought, there are still times where it can be extremely helpful to understand some coding so that you can get your devices to do what you want them to.

While I don’t teach computer science, I do teach my students many of the same concepts that they will need in order to write a computer program.  In many ways, writing and solving equations is equivalent to writing and performing a code you would want a computer to do.  Here, I will go over some of the ways I’ve been able to explain these processes to (college) students.

Algorithms

The first thing that people tend to forget about computers, is that they really aren’t intelligent.  They are just really good at following directions and performing the same task repetitively.  This is both wonderful and extremely frustrating at the same time.  That is, if you tell a computer to do something, it will do exactly as you say, even if you don’t say what you actually mean.  In this way, in order to be able to work with machines, you really need to know how to explain exactly what you want done and what order it should be done in.

The instructions that you would provide to a computer, or a person, in order to have a task completed is called an algorithm.  The first step in being able to write an algorithm is being able to follow one.  For historical context, the word algorithm is derived from al-Khwarizmi (780-850), the author of al-Jabr (which later turned into algebra) (A history of mathematics).  In al-Jabr, al-Khwarizmi describes to the reader the process required to solve quadratic equations.  Because of this, the algorithm I choose for my students to go through first is one similar to what was described in al-Jabr.  That is, while I use modern notation and conventions, I use his technique to solve the quadratic equation.

In order to complete an algorithm, we will need some type of input.  After the process, we will then have an output.  We then have:

• Take input of $$ax^{2}+bx+c$$.
• Define $$a$$ as the coefficient of $$x^{2}$$, $$b$$ as the coefficient of $$x$$ and $$c$$ as the constant term in the equation.
• Take $$b$$ and divide by $$a$$, let this value now be $$b$$.
• Take $$c$$ and divide by $$a$$, let this value now be $$c$$.
• Take $$b$$ and divide by $$2$$, let this value now be $$b$$.
• Square $$b$$, assign this new value to $$y$$.
• Take $$c$$ and subtract $$y$$.  Assign this value to $$y$$.
• If $$y \geq 0$$, then
• Take the square root of $$y$$.  Assign this to $$y$$.
• Subtract the current value of $$b$$ from $$y$$.  Assign this value to $$y_{1}$$.
• Multiply $$y$$ by negative 1.
• Subtract the current value of $$b$$ from $$y$$.  Assign this value to $$y_{2}$$.
• Assign the pair $$(y_{1},y_{2})=y$$.
• else
• Assign $$y$$ the value, no answer.
• Output $$y$$.

If we follow this algorithm, we will have a pair of number $$y_{1},y_{2}$$ such that $$ay_{1}^{2}+by_{1}+c=ay_{2}^{2}+by_{2}+c=0$$ or we will be told there is no such answer.  Furthermore, we will have $$y_{1}=y_{2}$$ if there is only one solution to the equation.

This may not look like the quadratic equation that you have seen; however, if you leave $$a,b$$ and $$c$$ as is, the $$y_{1},y_{2}$$ will be precisely the solutions $$y_{1}=\frac{-b+\sqrt{b^{2}-4ac}}{2a}$$ and $$y_{2}=\frac{-b-\sqrt{b^{2}-4ac}}{2a}$$.  I like to use this as an initial example for two reasons.  First, as I let the students work in groups, they can check their work when they are done to see if they followed the algorithm correctly.  Secondly, I like to make the point to them that the written instruction may not appear to follow the same outline as an equation.  This can be because it might arrive at the answer a different way, or because it is strange to see equations in sentences.  We indeed have both of these factors to worry about in the above algorithm.

Having them write an algorithm

Now that I have had my students work through an algorithm, I now hand it off to them to try to write such an algorithm.  In order to do this I will split them up into groups again and have them move so that they cannot eavesdrop on each other.  I will then give each of them a task they need to describe and have them write an algorithm.  The other group will be given a different task they need to write an algorithm for.  At this point, I don’t tell them this; however, after they have been given time to complete writing the algorithms, I take the written portion from groups and give it to another group with an input, but no context and ask them to complete the task.

For this situation, some of the tasks I will give are,

• Suppose that you are given a set of real numbers $$\left\{x_{1},x_{2}, \ldots x_{n}\right\}$$.  Write an algorithm to explain how to find the smallest number in the list.  Note that your steps may only compare at most 2 numbers at a time (that is you can’t say pick the smallest number of the list).
• Suppose that you are given a natural number $$n$$.  Write an algorithm that will find the first $$n$$ Fibonacci numbers, that is, the sequence $$(1,1,2,3,5,8,11,17, \ldots)$$. (Note here, I tell them that the pattern will continue but not what the pattern is.  In the past I have given the pattern, but as I am used to writing algorithms, I tend to do this in such a way that it gives the answer away.)

These are my favorite two to work with, because, there are relatively few instructions that need to be written.  However, the students will need to describe how to interpret an input, assign variables along the way, repeat a process and describe when to stop the process.  In the writing process, I have found the students often tend to be quite cavalier in how they explains things.  They assume that they would be working with it, so they would know the context in which the instructions were written.

As the students work through the instructions, they will often have difficulties.  If they had difficulties in the first example, they would adjust their techniques to get where they wanted to go, or blame me for writing it poorly.  In this situation, they are left blaming each other for poor writing.  However, since they had just written an algorithm, they will see many similarities.  This usually forces them to realize and admit to their own mistakes.  Seeing how a lack of instruction can affect another person, they tend to be much more careful in the future as we study.

Conclusion

Being able to give and follow precise instructions in an extremely important skill for everyone to have.  The importance of these skills has only grown greater as our lives have become more intertwined with technology and devices that will follow our instructions.  As you try to develop these skills with your students, it will be a long process of trial and error (probably for you and them).  Starting off by giving them a problem to work on without the specific terminology used in algorithms or computer coding helps them to realize the importance of being precise as the go through instructions.  While people can often overcome small omissions, computers can’t.  We can simulate this by not giving students any context within which to work.  I have found this an extremely helpful way to begin algorithms and coding; however, there will be much more to do.

If you would like more teaching resources, we have the STEM and leaf resources available in the shop.  Here, I shared my experiences in college classes.  This would also be appropriate in high school, but we will look at techniques for younger students in the future as well.

1 thought on “Writing and performing algorithms”

This site uses Akismet to reduce spam. Learn how your comment data is processed.