Break it into pieces
Breaking a problem into smaller pieces
Where do we start?
We want gcd(375, 825), the greatest common divisor, straight from its definition: the largest number that divides both.
What is a sensible first piece of work?
What this lesson covers
The idea
To design an algorithm, first understand what is required and break the problem into smaller pieces we know how to solve.
Where do we start?
We want gcd(375, 825), the greatest common divisor, straight from its definition: the largest number that divides both.
What is a sensible first piece of work?
- Find all the divisors of each number
- Guess a big number and check it
- Divide 825 by 375
Solve each piece
The book breaks the problem into pieces we know how to solve: Piece 1: list the divisors of 375. Piece 2: list the divisors of 825. (Both are done below.) Piece 3: compare the two lists and find the largest number in both. Run piece 3.
Small pieces we can solve
To design an algorithm, first understand what is required, then break the problem into smaller pieces that we know how to solve.
For gcd(m, n): • We want the greatest common divisor of two numbers. • For this, we first compute the divisors of both numbers and note them down. • Then we compare the two collections and find the largest number present in both. For 375 and 825 this gives 75.
Notes
Understand what is required, then break the problem into smaller pieces you know how to solve. For gcd: divisors of each number, then the largest common one.
Check yourself
Which pieces make up the book's plan for gcd(m, n)?
divisors(375) = [1, 3, 5, 15, 25, 75, 125, 375] and divisors(825) = [1, 3, 5, 11, 15, 25, 33, 55, 75, 165, 275, 825]. What is the largest number in both lists?
Answer: 75
375 and 125 are not divisors of 825, but 75 is. So gcd(375, 825) = 75.
- List the divisors of m, list the divisors of n, then find the largest number in both lists — correct. Yes. Each piece is something we already know how to do.
- Multiply m and n, then divide by the larger one. That is not how the book breaks the problem up.
- List the multiples of m and n. The gcd is about divisors, not multiples.