Euclid's Algorithm
Once you've stepped through Euclid's algorithm by hand, you may want to see a few more examples without the tedious calculations. Feel free to enter whatever values you want into the first row to see it in action:
| 119 | ||
| 544 | 119 | 68 |
| 119 | 68 | 51 |
| 68 | 51 | 17 |
| 51 | 17 | 0 |
Implementation
export function euclids_algorithm(m: number, n: number) {
while (true) {
let r: number = m % n // E1
if (r == 0) return n // E2
// E3
m = n
n = r
}
}
export function euclids_algorithm(m: number, n: number) {
let r = m % n
if (r == 0) return n
return euclids_algorithm(n, r)
}
Algorithm Structure
Algorithm F (Factorial). Given a positive integer
- F1. [Initialize f.] Set
. - F2. [Is it 1?] If
, the algorithm terminates; is the answer. - F3. [Reduce.] Set
, , and go back to step F2.
flowchart LR
F1[F1. Initialize $$f$$.]
F2[Is it 1?]
F3[Reduce.]
Start --> F1 --> F2 -- No --> F3 --> F2
F2 -- Yes --> Result
Notation Reference
- Algorithm F
- Reference to an algorithm within the current section.
- Algorithm 1.1E
- Reference to an algorithm in a different section. For algorithms written on this site (like Algorithm F above) they will be referenced as W1.1F.
- The
assigns the result of an expression to a variable. In this case is assigned to the variable . - Check if
is equal to . - Swap the values of
and . - Assign variables
and to the result of the expression, in this case . - If condition, do something
- Perform "something" only if "condition" is true.
- Indicates the end of the algorithm.
- The
th element of array . - The
th element of the th array in the multi-dimensional array .
Algorithm Feature Reference
- Finiteness
- An algorithm eventually terminates.
- Definiteness
- Each step of an algorithm contains no ambiguity.
- Input
- An algorithm takes zero or more inputs and defines its constraints.
- Output
- An algorithm has at least one resulting value which relates to the input.
- Effectiveness
- Operations of an algorithm are simple enough that they can be worked out by hand.
Algorithm E Correctness Proof Explanation
Since E3 changes the values of
The typical strategy when proving equivalence of sets, say
- The set of divisors of
and . - The set of divisors of
and .
When Knuth points out that
So because
Worked Example of
Here are the operations involved in calculating the individual values for
Explanation of Set Theory Grounding
To understand the set theory grounding, consider step E3. The information delivered to the step, along with the step number itself, form a packet of information describing the state of the machine executing the algorithm at that point:
| Step | |||
|---|---|---|---|
This state is one element of
| Step | |||
|---|---|---|---|
Every combination of values for each of these steps forms additional elements of the set
| Step | ||||
|---|---|---|---|---|
Because E2 can branch depending on the value of
| Step | ||||
|---|---|---|---|---|
Notice how information is stripped from
Let us look again at the example worked in the book of
| Step | Equation | ||||
|---|---|---|---|---|---|
This sequence
For this mathematical model to be an algorithm, it must meet the five criteria
outlined earlier in the section. As mathematical equations, they are by their
nature definite, and the sets
Effectiveness must also be achieved via additional constraints, but this can be
accomplished by a number of different approaches. The discussion around
Using
Let's look at how to implement addition with the framework Knuth set forth to
understand how it works. If we want to represent
Transformations only occur when
Now consider multiplication, which is a little more complicated to model. To
begin, we want to take a
This can be done with
For
Applying this process repeatedly will eventually reach
Here is a tool to explore this more fully:
| 0 | ||||
| 1 | ||||
| 2 | ||||
| 3 |
Enter a string below to watch the algorithm above apply to it, one stage at a time:
| String | ||
|---|---|---|
| a3b5 | 0 | |
| 4 | bacacacb4 | 1 |
| acacacb4 | 2 | |
| c3a3b4 | 0 | |
| 16 | c3bacacacb3 | 1 |
| c3acacacb3 | 2 | |
| c6a3b3 | 0 | |
| 28 | c6bacacacb2 | 1 |
| c6acacacb2 | 2 | |
| c9a3b2 | 0 | |
| 40 | c9bacacacb | 1 |
| c9acacacb | 2 | |
| c12a3b | 0 | |
| 52 | c12bacacac | 1 |
| c12acacac | 2 | |
| 60 | c15a3 | 0 |
| 61 | c15a3 | 1 |
| c15a3 | 3 | |
| 66 | c15 |
Exercise and Answer Clarifications
3. It's unclear from the problem statement that assignment isn't entirely
disallowed. So even though
6. This is asking you to replicate the calculation for each possible value of