Still too slow
Scanning every value is slow for large numbers
How much work?
Even our best scan checks every number from 1 to min(m, n). For 375 and 825 that is 375 checks.
If the smaller number grows from 3 digits to 5 digits, the work grows …
What this lesson covers
The idea
Checking every number up to min(m, n) takes work that grows with the value of the numbers, not with their number of digits.
How much work?
Even our best scan checks every number from 1 to min(m, n). For 375 and 825 that is 375 checks.
If the smaller number grows from 3 digits to 5 digits, the work grows …
- about 100 times
- by 2 more checks
- it stays the same
A big scan
Run the scan for gcd(54000, 81000). Watch the number of checks.
Value, not digits
Checking every number up to min(m, n) takes work that grows with the value of the numbers, not with their number of digits.
This is like adding by counting dots. If the smaller number goes from 3 digits to 4 digits, we scan 10 times as many values; from 3 digits to 5 digits, 100 times as many. For 54000 and 81000: about 54000 checks. The book asks: as for adding, can we find a gcd algorithm whose effort grows with the number of digits?
Notes
Scanning up to min(m, n) is slow for large numbers: its work grows with the value. We want work that grows with the number of digits.
Check yourself
At one check per second, about how many hours does the scan for gcd(54000, 81000) take? (Round to the nearest hour.)
Answer: 15
54000 ÷ 3600 = 15 hours.
The smaller number goes from 3 digits to 4 digits. The scan's work grows …
- about 10 times — correct. Yes, like the number of dots in addition.
- by about one check. That is what we want, but this algorithm does not do it.
- about 100 times. 100 times is for 3 digits to 5 digits.