Algorithm Formula

The Formula

\text{output} = f(\text{input})

When to use: A recipe for solving problems—follow the steps, get the answer.

Quick Example

A recipe for making a sandwich, directions to get somewhere, long division steps.

What This Formula Means

A step-by-step set of instructions for solving a problem or accomplishing a specific task. An algorithm must be precise (every step is unambiguous), finite (it terminates after a bounded number of steps), and effective (each step can actually be carried out).

A recipe for solving problems—follow the steps, get the answer.

Formal View

An algorithm is a finite sequence of well-defined instructions that, given valid input, produces the correct output and terminates in a finite number of steps.

Worked Examples

Example 1

easy
Write an algorithm (as numbered steps) for making a cup of tea.

Solution

  1. 1
    Step 1: Identify the key actions in order — boil water, prepare the cup, brew, and serve.
  2. 2
    Step 2: Write clear, unambiguous steps: 1. Fill kettle with water. 2. Boil the kettle. 3. Place tea bag in cup. 4. Pour boiling water into cup. 5. Wait 3 minutes. 6. Remove tea bag. 7. Add milk if desired.
  3. 3
    Step 3: Verify the algorithm is complete (handles start to finish) and each step is precise enough for anyone to follow.

Answer

A 7-step algorithm from filling the kettle to adding milk, with each instruction clear and ordered.
An algorithm is a step-by-step set of instructions to solve a problem or complete a task. Good algorithms are precise, unambiguous, and finite.

Example 2

medium
Here is an algorithm: 1. Set total = 0. 2. For each number in the list [3, 7, 2, 8]: add the number to total. 3. Output total. What does this algorithm compute?

Common Mistakes

  • Writing steps that are ambiguous or assume human judgment the computer cannot replicate
  • Forgetting to handle edge cases such as empty input or extreme values
  • Creating an algorithm that works for one example but fails on other valid inputs

Common Mistakes Guide

If this formula feels simple in isolation but keeps breaking during real problems, review the most common errors before you practice again.

Why This Formula Matters

All computer programs are algorithms—understanding them is understanding computing. From search engines ranking billions of web pages to GPS finding the fastest route, algorithms power every piece of technology you use daily.

Frequently Asked Questions

What is the Algorithm formula?

A step-by-step set of instructions for solving a problem or accomplishing a specific task. An algorithm must be precise (every step is unambiguous), finite (it terminates after a bounded number of steps), and effective (each step can actually be carried out).

How do you use the Algorithm formula?

A recipe for solving problems—follow the steps, get the answer.

Why is the Algorithm formula important in CS Thinking?

All computer programs are algorithms—understanding them is understanding computing. From search engines ranking billions of web pages to GPS finding the fastest route, algorithms power every piece of technology you use daily.

What do students get wrong about Algorithm?

Algorithms must work for all valid inputs, not just specific examples.