Improving the Algorithm
Last updated at October 28, 2026 by Teachoo
Transcript
Improving the Algorithm Letβs try to improve our gcd algorithm 1. The original GCD algorithm To find gcd(π,π), we previously: Checked every number from 1 to π to find the divisors of π. Checked every number from 1 to π to find the divisors of π. Compared the two lists to obtain the common divisors. Reported the rightmost common divisor, because the list was increasing. For example, to find gcd(6,12) : divisors(6)=[1, 2, 3, 6] divisors(12)=[1, 2, 3, 4, 6, 12] Their common divisors are: [1, 2, 3, 6] Therefore: gcd(6,12)=6 This works, but it creates two divisor lists and a third common-divisor list. It also runs through many of the same candidate numbers twice. divisors(12)=[1, 2, 3, 4, 6, 12] Their common divisors are: [1, 2, 3, 6] Therefore: gcd(6,12)=6 This works, but it creates two divisor lists and a third common-divisor list. It also runs through many of the same candidate numbers twice. 2. First improvement: Compute both divisor lists together Instead of making two separate scans, we can make one scan and check each candidate against both numbers. For this, we use: π¦ππ±(π,π) which means the greater of π and π. For example: max(6, 12)=12 Updated algorithm Start with two empty lists: divisors-of-m and divisors-of-n. For each number π from 1 to max(π,π) : If π divides π, add it to divisors-of-m. If π divides π, add it to divisors-of-n. Compare the lists to find the common divisors. Report the greatest common divisor. Example: π=π,π=ππ π¦ππ±(π,π) which means the greater of π and π. For example: max(6, 12)=12 Updated algorithm Start with two empty lists: divisors-of-m and divisors-of-n. For each number π from 1 to max(π,π) : If π divides π, add it to divisors-of-m. If π divides π, add it to divisors-of-n. Compare the lists to find the common divisors. Report the greatest common divisor. Example: π=π,π=ππ Example: π=π,π=ππ We obtain the same two divisor lists, but through one combined scan. What changed? Previously, we made: 6+12=18 candidate visits across two scans. Now, we make: max(6,12)=12 candidate visits in one scan. But, it isnβt efficient The combined scan has fewer iterations, but each iteration may perform two divisibility tests. Therefore, combining the scans alone does not necessarily reduce the total number of division operations. The next improvements remove more substantial unnecessary work. 3. Second improvement: Find common divisors directly Our goal is the GCD. Do we really need separate lists of divisors? No. We only need numbers that divide both numbers So, for each candidate π, directly ask: Does π divide both π and π ? Updated algorithm Start with an empty list common-divisors. For each number π from 1 to max(π,π) : If π divides both π and π, add it to common-divisors. Report the rightmost entry as the GCD. For 6 and 12, we directly obtain: [1, 2, 3, 6] We no longer create two separate divisor lists or compare them afterwards. 4. Third improvement: Check only up to the smaller number Can a common divisor of 6 and 12 be greater than 6 ? No. A positive divisor of 6 cannot exceed 6 More generally, a common divisor must divide both numbers, so it cannot exceed the smaller one. We therefore only need to check up to: π¦π’π§(π,π) where min(π,π) means the smaller number. For example: min(6,12)=6 Updated algorithm Start with an empty list common-divisors. For each number π from 1 to min(π,π) : If π divides both π and π, add it to common-divisors. Report the rightmost entry as the GCD. For 6 and 12, we check only: 1, 2, 3, 4, 5, 6 There is no need to check 7, 8,β¦,12 The upper limit changed from: π¦ππ±(π,π) to π¦π’π§(π,π). For numbers such as 6 and 1200 , this is a considerable reduction: only six candidate values need to be considered. 5. Fourth improvement: Do away with lists We are looking for the greatest common divisor, not the complete list of common divisors. Suppose we check candidates in increasing order and discover: 1, 2, 3, 6 Now, Once we find 3 , the previously found values 1 and 2 cannot be the greatest. Once we find 6 , the earlier value 3 is no longer needed either. Therefore: Keep only the most recently found common divisor. Because candidates are examined in increasing order, this is also the greatest common divisor found so far. We can name this stored value: most-recent-common-divisor We put its default value as 1 , because 1 divides every positive integer. Final improved algorithm for gcd Set most-recent-common-divisor to 1. For each number π from 2 to min(π,π) : If π divides both π and π, set most-recent-common-divisor to π. Report most-recent-common-divisor as gcd(π,π) Example: Find π ππ(π,ππ) Initially: most-recent-common-divisor = 1 Candidate j | Divides 6? | Divides 12? 1 | Yes | Yes 2 | Yes | Yes 3 | Yes | Yes 4 | No | Yes 5 | No | No 6 | Yes | Yes 7, 8, 9, 10, 11 | No | No 12 | No | Yes candidate visits across two scans. Now, we make: max(6,12)=12 candidate visits in one scan. But, it isnβt efficient The combined scan has fewer iterations, but each iteration may perform two divisibility tests. Therefore, combining the scans alone does not necessarily reduce the total number of division operations. The next improvements remove more substantial unnecessary work. 3. Second improvement: Find common divisors directly Our goal is the GCD. Do we really need separate lists of divisors? No. We only need numbers that divide both numbers So, for each candidate π, directly ask: Does π divide both π and π ? Updated algorithm Start with an empty list common-divisors. For each number π from 1 to max(π,π) : If π divides both π and π, add it to common-divisors. Report the rightmost entry as the GCD. For 6 and 12, we directly obtain: [1, 2, 3, 6] We no longer create two separate divisor lists or compare them afterwards. 4. Third improvement: Check only up to the smaller number Can a common divisor of 6 and 12 be greater than 6 ? No. A positive divisor of 6 cannot exceed 6 More generally, a common divisor must divide both numbers, so it cannot exceed the smaller one. We therefore only need to check up to: π¦π’π§(π,π) where min(π,π) means the smaller number. For example: min(6,12)=6 Updated algorithm Start with an empty list common-divisors. For each number π from 1 to min(π,π) : If π divides both π and π, add it to common-divisors. Report the rightmost entry as the GCD. For 6 and 12, we check only: 1, 2, 3, 4, 5, 6 There is no need to check 7, 8,β¦,12 The upper limit changed from: π¦ππ±(π,π) to π¦π’π§(π,π). For numbers such as 6 and 1200 , this is a considerable reduction: only six candidate values need to be considered. 5. Fourth improvement: Do away with lists We are looking for the greatest common divisor, not the complete list of common divisors. Suppose we check candidates in increasing order and discover: 1, 2, 3, 6 Now, Once we find 3 , the previously found values 1 and 2 cannot be the greatest. Once we find 6 , the earlier value 3 is no longer needed either. Therefore: Keep only the most recently found common divisor. Because candidates are examined in increasing order, this is also the greatest common divisor found so far. We can name this stored value: most-recent-common-divisor We put its default value as 1 , because 1 divides every positive integer. Final improved algorithm for gcd Set most-recent-common-divisor to 1. For each number π from 2 to min(π,π) : If π divides both π and π, set most-recent-common-divisor to π. Report most-recent-common-divisor as gcd(π,π) Example: Find π ππ(π,ππ) Initially: most-recent-common-divisor = 1 min(6,12)=6 Updated algorithm Start with an empty list common-divisors. For each number π from 1 to min(π,π) : If π divides both π and π, add it to common-divisors. Report the rightmost entry as the GCD. For 6 and 12, we check only: 1, 2, 3, 4, 5, 6 There is no need to check 7, 8,β¦,12 The upper limit changed from: π¦ππ±(π,π) to π¦π’π§(π,π). For numbers such as 6 and 1200 , this is a considerable reduction: only six candidate values need to be considered. 5. Fourth improvement: Do away with lists We are looking for the greatest common divisor, not the complete list of common divisors. Suppose we check candidates in increasing order and discover: 1, 2, 3, 6 Now, Once we find 3 , the previously found values 1 and 2 cannot be the greatest. Once we find 6 , the earlier value 3 is no longer needed either. Therefore: Keep only the most recently found common divisor. Because candidates are examined in increasing order, this is also the greatest common divisor found so far. We can name this stored value: most-recent-common-divisor We put its default value as 1 , because 1 divides every positive integer. Final improved algorithm for gcd Set most-recent-common-divisor to 1. For each number π from 2 to min(π,π) : If π divides both π and π, set most-recent-common-divisor to π. Report most-recent-common-divisor as gcd(π,π) Example: Find π ππ(π,ππ) Initially: most-recent-common-divisor = 1 For numbers such as 6 and 1200 , this is a considerable reduction: only six candidate values need to be considered. 5. Fourth improvement: Do away with lists We are looking for the greatest common divisor, not the complete list of common divisors. Suppose we check candidates in increasing order and discover: 1, 2, 3, 6 Now, Once we find 3 , the previously found values 1 and 2 cannot be the greatest. Once we find 6 , the earlier value 3 is no longer needed either. Therefore: Keep only the most recently found common divisor. Because candidates are examined in increasing order, this is also the greatest common divisor found so far. We can name this stored value: most-recent-common-divisor We put its default value as 1 , because 1 divides every positive integer. Final improved algorithm for gcd Set most-recent-common-divisor to 1. For each number π from 2 to min(π,π) : If π divides both π and π, set most-recent-common-divisor to π. Report most-recent-common-divisor as gcd(π,π) Example: Find π ππ(π,ππ) Initially: most-recent-common-divisor = 1 Keep only the most recently found common divisor. Because candidates are examined in increasing order, this is also the greatest common divisor found so far. We can name this stored value: most-recent-common-divisor We put its default value as 1 , because 1 divides every positive integer. Final improved algorithm for gcd Set most-recent-common-divisor to 1. For each number π from 2 to min(π,π) : If π divides both π and π, set most-recent-common-divisor to π. Report most-recent-common-divisor as gcd(π,π) Example: Find π ππ(π,ππ) Initially: most-recent-common-divisor = 1 Report most-recent-common-divisor as gcd(π,π) Example: Find π ππ(π,ππ) Initially: most-recent-common-divisor = 1 Candidate k | Does it divide both numbers? | most-recent-common-divisor 2 | Yes (6)/(2) = 3, (12)/(2) = 6 | 2 3 | Yes (6)/(3) = 2, (12)/(3) = 3 | 3 4 | No: it does not divide 6 | 3 5 | No | 3 6 | Yes (6)/(6) = 1, (12)/(6) = 2 | 6 Candidate k | Does it divide both numbers? | most-recent-common-divisor 2 | | 2 3 | | 3 4 | No: it does not divide 6 | 3 5 | No | 3 6 | | 6 After all candidates have been checked: π ππ(π,ππ)=π Why does this algorithm work? Every possible common divisor is at most min(π,π). We check all candidates in that range. Whenever a new common divisor is found, it is greater than the previous one. Therefore, the final stored value is the greatest common divisor. If the GCD is 1 , no larger common divisor is found, so the stored value correctly remains 1 .