text stringlengths 0 897 |
|---|
A> **Chapter Objectives** |
A> - Learn about algorithms and pseudocode. |
A> - Translate several pseudocode algorithms into MiniScript. |
A> - See how to fix an incomplete algorithm. |
Today we're going to address head-on a topic we've only hinted before now: *algorithms*. |
algorithm |
: a process to be followed in order to solve a problem |
Algorithms are wonderful things. They are clear, step-by-step instructions for accomplishing something. In mathematics, you learned algorithms for multiplying or dividing big numbers. In the kitchen, you might have an entire book of algorithms for making various foods (those algorithms are usually called "recipes").... |
Of course programming is also telling a computer specifically what to do. We've been doing that this whole book. But programming is giving instructions to a computer in a specific programming language, like MiniScript. Algorithms are at a slightly more generic level; they are instructions on how to solve a problem r... |
pseudocode |
: a made-up, natural-ish programming language used to convey an algorithm to human readers |
You can find algorithms for all sorts of things. Need to efficiently combine two sorted lists into one bigger list that's still sorted? There's an algorithm for that. Need to divide a bunch of data points into clusters? There's one for that too. Want to find the first 1000 prime numbers? You can find several diff... |
D> Programming languages come and go, but algorithms are eternal. |
So in the end, an algorithm is just a fancy word for a simple concept: step-by-step instructions on how to do something. Let's make all this concrete by looking at a few simple, well-known algorithms. |
## Greatest Common Divisor |
The greatest common divisor (or "GCD" for short) of two numbers is the largest whole number that will divide evenly into them both. For example, the GCD of 8 and 12 is 4, because 4 divides both 8 and 12 without any fractions or remainder. The GCD has all sorts of uses. For example, if you want to simplify a fraction... |
A pretty neat algorithm for finding the GCD was developed by Euclid, a Greek mathematician who lived around 300 BC. This is one of the oldest algorithms still in common use. If you search for "Euclidean algorithm for greatest common divisor," you will find a whole lot of material about it, including a Wikipedia page ... |
```pseudo |
function gcd(a, b) |
while b != 0 |
t := b |
b := a mod b |
a := t |
return a |
``` |
I bet you can read that! It looks *almost* like MiniScript, but not quite. It uses `:=` to indicate assignment, while we use a simple `=` sign. It's written `mod` as a word, while in MiniScript we use the `%` operator. And finally, you may notice that the `while` loop does not have an `end while`, and the `function... |
```miniscript |
gcd = function(a, b) |
while b != 0 |
t = b |
b = a % b |
a = t |
end while |
return a |
end function |
``` |
Pretty direct conversion, isn't it? Type that in and try it out. What is the GCD of 294 and 546? |
Skipping ahead to the third version of the algorithm, the pseudocode is given as: |
```pseudo |
function gcd(a, b) |
if b = 0 |
return a |
else |
return gcd(b, a mod b) |
``` |
This is a fun one because it's recursive — it is defined in terms of itself. But as MiniScript supports recursion too, this too is a straightforward translation: |
```miniscript |
gcd = function(a, b) |
if b == 0 then |
return a |
else |
return gcd(b, a % b) |
end if |
end function |
``` |
D> Remember that MiniScript uses `==` for comparing two numbers. Pseudocode often uses a single `=` (especially pseudocode that uses `:=` for assignment). |
And finally, the second version in the Wikipedia page is actually Euclid's original version, as he hadn't thought of *mod*. This is given in pseudocode as: |
```pseudo |
function gcd(a, b) |
while a != b |
if a > b |
a := a - b |
else |
b := b - a |
return a |
``` |
Can you translate this to MiniScript code yourself? Try it! |
## Binary Search |
Another useful algorithm is finding the position of an element in a sorted list. Of course you could just search every element with this algorithm: |
```pseudo |
function find(x, list) |
i := 0 |
while i < length of list |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.