# Algorithms and Computational Thinking

Mathematics I · Programming Elements · https://tryals.app/learn/mathematics-i/algorithms-and-computational-thinking

## What is an Algorithm?

An **algorithm** is a finite, precise sequence of unambiguous steps that solves a problem. It must be **finite** (it terminates), **definite** (each step is clear), and **effective** (each step is doable).

**Computational thinking** is the mindset behind algorithm design. Its core habits:

| Habit | What it means |
| --- | --- |
| **Decomposition** | Break big problems into subproblems |
| **Pattern recognition** | Spot repeated structures |
| **Abstraction** | Hide irrelevant detail |
| **Algorithm design** | Express solution as steps |

Before coding, use **pseudocode** (language-independent English) or flowcharts to plan logic.

## How to Compute vs What It Is

A mathematical **definition** states what an answer is, but an **algorithm** gives a concrete procedure for **computing** it.

| Concept | Role | Example |
| --- | --- | --- |
| **Definition** | What the answer is | "Largest common divisor" |
| **Algorithm** | How to compute it | Euclid's GCD method |

> **Common pitfall:** Confusing a **specification** with an **algorithm**. Saying "prime factorisation is the unique product of primes" only specifies the answer; you still need a terminating procedure like trial division to produce it.

## Practice questions

5 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. Order the steps of the algorithm that finds the largest value in a list.

**Answer:**

1. Set best equal to the first element of the list
2. Look at the next element in the list
3. If that element is greater than best, set best to it
4. Repeat until no elements remain
5. Report best as the maximum

**Why:** You must initialise before you can compare, compare before you can update, and exhaust the list before you can be sure. Starting best at zero instead of the first element is the usual bug, it fails on a list of negative numbers.

Page: https://tryals.app/practice/mathematics-i/algorithms-and-computational-thinking/order-the-steps-of-the-algorithm-that-finds-the-largest-value-in-a

### 2. A recipe is a precise sequence of steps and is not an algorithm in the strict sense. Which requirement does *"season to taste"* violate?

A. Input, since taste is not supplied as a parameter
B. Definiteness, since 'to taste' leaves the executor to decide
C. Finiteness, since seasoning could continue indefinitely
D. Effectiveness, since seasoning cannot be carried out mechanically

**Answer:** B. Definiteness, since 'to taste' leaves the executor to decide

**Why:** Two cooks execute it differently, so the step is not definite, and a procedure whose behaviour depends on who runs it cannot be reasoned about or reproduced. That is exactly why programming languages are unforgiving: definiteness is a requirement, and 'you know what I mean' is not available.

Page: https://tryals.app/practice/mathematics-i/algorithms-and-computational-thinking/a-recipe-is-a-precise-sequence-of-steps-and-is-not-an-algorithm-in

### 3. Select every procedure below that FAILS to be a genuine algorithm.

A. Add 1 to n, then stop
B. Pick whichever move looks best and play it
C. Divide n by each integer from 2 to n-1 and report whether any divides evenly
D. The answer is the smallest x satisfying the equation
E. Start with x = 1 and repeatedly double it

**Answer:** B. Pick whichever move looks best and play it; D. The answer is the smallest x satisfying the equation; E. Start with x = 1 and repeatedly double it

**Why:** Doubling forever fails finiteness; 'looks best' fails definiteness; 'the smallest x satisfying the equation' is a specification with no procedure. The trial-division test and 'add 1 then stop' are both finite, definite and effective, so both are algorithms.

Page: https://tryals.app/practice/mathematics-i/algorithms-and-computational-thinking/select-every-procedure-below-that-fails-to-be-a-genuine-algorithm

### 4. A specification says WHAT the answer must be; an algorithm says HOW to get it. Why is the distinction worth maintaining, when a correct algorithm satisfies its specification anyway?

A. Because one specification admits many algorithms of different cost.
B. Because algorithms may be incorrect, whereas specifications are correct by definition.
C. Because specifications are written for users and algorithms for machines, so the two never appear together.
D. Because a specification cannot be tested, while an algorithm can be run.

**Answer:** A. Because one specification admits many algorithms of different cost.

**Why:** 'Return the largest element' is satisfied by a single pass in n steps, by sorting in n log n, and by comparing every pair in n², all correct, and the specification cannot tell them apart. So correctness is the floor and choosing among the correct options is the design work. It also makes improvement safe, which is the same insight the abstract data type lesson makes about interfaces.

Page: https://tryals.app/practice/mathematics-i/algorithms-and-computational-thinking/a-specification-says-what-the-answer-must-be-an-algorithm-says-how

### 5. An algorithm says HOW to compute; a specification says only WHAT the answer is. Sort each item.

**Answer:**

- Algorithm: Replace (a, b) by (b, a mod b) until b is 0, then report a, Test each of 2, 3, ..., n-1 as a divisor of n; if none divides, report n prime, Scan the list keeping the largest value seen so far, then report it
- Specification only: The gcd of a and b is their largest common divisor, The prime factorisation is the unique product of primes equal to n

**Why:** a1, a3 and a5 give concrete terminating procedures. a2 and a4 characterise the answer without saying how to find it, the classic confusion of this lesson. A definition becomes an algorithm only when it is turned into steps.

Page: https://tryals.app/practice/mathematics-i/algorithms-and-computational-thinking/an-algorithm-says-how-to-compute-a-specification-says-only-what-the
