Improving the Algorithm - Class 9 Maths [The World of Algorithms] - Improving the Algorithm

part 2 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

part 3 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

part 4 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

part 5 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

part 6 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

part 7 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

part 8 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

part 9 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

part 10 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

part 11 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

part 12 - Improving the Algorithm - Improving the Algorithm - Chapter 11 Class 9 - The World of Algorithms (Ganita Manjari) - Maths Class 9

Remove Ads
Teachoo Β· Class 9 Explore Class 9

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 .

CA Maninder Singh's photo - Co-founder, Teachoo

Made by

CA Maninder Singh

CA Maninder Singh is a Chartered Accountant qualified since 2010 and an educator teaching since 2006. At Teachoo, he draws on his accounting and tax experience to explain Accounts, Income Tax and GST through step-by-step lessons and practical examples.

For an uninterrupted learning experience, students can use Teachoo Black to remove ads and focus better.