“Those who can imagine anything, can create the impossible.” - Alan Turing
Algorithms are mathematical problems that can cause amateur mathematicians to break out in a cold sweat. Now algorithms are part of our daily lives and stretch well beyond just IT. A recipe, puzzle, or even a sports training routine could be considered algorithms.
In 2019, there were an estimated 19 million programmers around the world. There’s expected to be around 45 million by the year 2030.
What better reason to learn to write an algorithm?
In this article, the Superprof blog is looking at maths tips for coding an algorithm.
What Is an Algorithm?
The first time you’ll see an algorithm is probably when you start studying geometry.
Even your times tables can serve as an introduction to algorithms since an algorithm is a set of instructions used to find a result in a database of known information.
To create an algorithm, you need to be able to answer these questions:
- What is a variable?
- What types of variables are possible?
- What kind of instructions can I use to sort the operations?
Since the goal of an algorithm is to repeat a set of instructions until the final result is found from known and unknown information, you’ll need to define unknown variables.
A variable is data stored that a program will seek to find and can be numerical (a number), textual (characters), or boolean (with a true or false value).
For example: to make a dish, the chef needs to follow the recipe step by step until it’s done.
In cooking, you could say “add salt”, “the dish is too salty”, “stop adding salt”.
Replace the ingredients with triangles, rectangles, vectors, and equations and you’ve got logic.
Let’s get back to our calculations...
The instructions need to be in a language that a computer can understand.
In algorithms, the languages are called pseudo-code as it’s neither a programming language nor a natural language. It’s a way to describe algorithms without necessarily needing to know how to code. The objective is to describe the processes or instructions that will be carried out.
For example, when crossing the road, your brain executes the following algorithm:
- Look right, left, and right again.
- If there’s no car,
- Cross the road.
- Otherwise, wait and repeat.
In mathematical terms, you need to describe what happens in the opposite case.
You’ll need to use iterators and conditions.
An algorithm includes 3 stages:
- The data needed to use the algorithm.
- The operations to execute.
- The final result.
Coding algorithms requires an understanding of logic.
Defining the Variables to Write an Algorithm
A variable, by definition, is data whose value can change, fluctuate, or vary.
For example, on Twitter, tweets are considered variables. This message can be filled with up to 280 characters. The variable, in this case, is the “tweet” and is stored as text.
A variable can also be made up of decimal numbers: 0.1, 0.011, 0.12, ..., 0.999, etc. and it should be known that programs can only store whole numbers, real numbers, and complex numbers, and don’t recognise letters.
To create algorithms, you need to specify what each type of variable is. The goal is to describe the type of situation the number will be found in.
For example, to program an alarm, you need to order the phone to ring at 6:00 from Monday to Friday. Instead of sounding the alarm every day and then exclude Saturday and Sunday, you can set it to sound every day from Monday to Friday inclusive.
You could use operators to indicate these variables:
- For numbers, you can use signs like +, -, x, and ÷.
- For strings of characters, you can use & and + to join strings.
- For booleans, you can use logic like AND, OR, or NOT.
An algorithm can repeat on a loop until solved.
Conditional and Iterative Statements
An iterative statement repeats a given number of times.
To get to the final result, the algorithm will loop many times, often using terms as FOR or WHILE in code. This means it will repeat until the result is what it needs.
For example, with revision, you could use a similar process. While you’re still making mistakes, keep studying. By the end, the condition will have been met and you can end the loop, as long as you haven’t created a condition that causes an infinite loop.
In these cases, “ENDIF”, “IFEND”, and “END” are commonly used to break otherwise endless loops.
What Tools Can You Use for Algorithms
Secondary school students may be tasked with writing algorithms. Schools are teaching younger and younger students about coding and algorithms.
Discover different Maths tutor on Superprof.
Younger secondary school students can use tools such as:
For more advanced or older students, there are tools such as:
- Beetle Blocks
Scratch is one of the most popular tools. It allows children between ages 8 and 16 to learn about coding and algorithms. After all, programming and algorithms are closely related and scratch allows students to program their own interactive stories, games, and animations. It’s available for free and is open source.
For older students, Python is the language to learn when it comes to programming algorithms. GCSE and A Level students can start learning this programming language. Furthermore, there are plenty of great online resources for programming algorithms with Python.
Exercising for Practising with Algorithms
There’s no better way to understand something than by trying it out for yourself.
Here are some quick exercises you can do to make basic algorithms.
Imagine we have 100 toy cubes. A child is building “pyramids” with them.
- Step 1: 1 cube.
- Step 2: 3 cubes (2 on the base and 1 on the 2nd level)
- Step 3: 6 cubes (3 on the base, 2 on the 2nd level, and 1 on the 3rd level)
How many times can we repeat this process until all the cubes have been used?
Determine the number of cubes needed for step 4 and step 5.
How many cubes will be needed to go from step 3 to step 4, then from step 4 to step 5, then from step n-1 to step n?
Complete this algorithm with N being the number of steps you can carry out with 100 cubes and R is the number of cubes remaining.
Make an algorithm that chooses a number between 1 and 5 until it gets the desired number:
Here’s how this should be written out:
- Variable N is a Whole Number
- N ← 0
- Write “Enter a number between 1 and 5”
- WHILE N < 1 OR > 5 READ N.
- IF N < 1 OR N > 5
- WRITE “Wrong. Try again.”
Feeling a bit lost?
Ready to learn more about algorithms?
Fortunately, Superprof is here to help. Don’t hesitate to look for a programming tutor to help you. There’s no better way to learn how to code than with tailored tutorials and exercises to help you practise.
You can find tutors specialising in maths for all levels from primary school to university. There are different ways to learn from a private tutor so make sure you choose the type of tutoring that works for you, how you like to learn, and your budget.
Face-to-face tutoring is the most common and usually involves the tutor teaching just one student at their home. Since there's only one student, the tutor can tailor every minute of the lesson to them and ensure they're getting the most out of every minute they're working together. Of course, this bespoke service tends to cost more as you're paying for the tutoring and the time the tutor has to spend planning the lessons and travelling to their students' homes.
Online tutorials can also be taught one-on-one, but since the tutor doesn't have to travel to their students and can teach more lessons each week, they don't tend to charge as much. While these aren't ideal for hands-on subjects and skills, online tutoring is excellent for academic subjects such as maths.
Group tutorials are an excellent choice for those on a budget. With several students attending the same class, the tutor can afford to charge less per student. While you won't get to enjoy lessons that are tailored just to you, you can enjoy paying less for them. If you and some friends, family members, classmates, or colleagues need to learn more about maths, group tutoring could be an excellent and affordable option.
Don't forget that many of the tutors on Superprof offer the first hour of tutoring for free so you can try a few out before deciding on which one is right for you. You could also try out the different types of tutoring if you're not sure which one you'd prefer.