Designing Different Algorithms
Let's devise different algorithms related to the greatest common divisors and binary systems.
Greatest Common Divisor
A factor is a number that divides a larger number evenly. Two factors can be multiplied with each other to get another number.
A number can even have multiple factors. For example,
GCD or the highest common factor is the largest factor among the factors of the two numbers.
is the largest common factor for and hence,
Application of GCD
Below is one example application of the GCD.
Lemonade stand problem
We need to help a lemonade stand owner in providing the required quantity of lemonade as per the customer’s will. He has two containers with -liter and -liter capacity, but there is no measurement mark on either of the containers. To provide 5 or 8 liters of lemonade to customers, it would be very straightforward. But if a customer demands either , then what should the methodology be?
Solution
We can fill either container with lemonade and then pour it into the other one to get the desired quantity.
1 liter
- First of all, we need to fill the container and pour that into the smaller one with capacity. As a result, only liters will be left in the container.
- Empty the container and pour the remaining liters from the container into it.
- Now, fill the container again, and pour that into the smaller one that already contains liters. As a result, liters will be left in an container.
- We have to empty the container and pour the container, which already has liters, into it. This procedure will leave liter lemonade in the container. We can give it to the customer.
2 liters
- Fill the container and pour it into the larger container with capacity. Now more liters can be added to the larger container.
- Fill the container again and pout into the container where the available empty capacity is liters. As result, liters will be left in a container, which can be forwarded to the customer.
Can we do the same if we had 6-liter and 9-liter containers?
The example above is a very befitting application of GCD that depicts how we could measure 1, 2, 3, 4, 5, and 6 liters with the 5-liter and 8-liter containers (since the GCD of 5 and 8 is 1). Had it been 6-liter and 8-liter containers, we could only measure ranges that were multiples of 2 (since the GCD of 6 and 8 is 2).
GCD is also used to compute modular inverses, which are important in many encryption schemes, including RSA.
Binary number system
It’s a number system with base as it only contains two alphabets, and , to represent any number.
The binary number system starts with , and the next number can be obtained by adding to it. So, the 2nd number will be . For the next number, , we keep to the right and carry to the left. And so on.
Remember: In computers, a binary number system is used where we can switch off the transistor (case-0) or switch on the transistor (case-1) by providing the required voltages.
Application of binary number system
We need to create and print money bills of any amount between and in order to create a currency system that can provide change for any transaction within this range, and it should have the least effect on the environment.
Solution 1
Any transaction can be processed by creating money bills of each.
But there is a drawback – the more money bills we print, the higher percentage of trees will be utilized. Hence, it is not an environment-friendly solution.
Solution 2
If we create money bills of each and money bills of each, we can achieve our goal. In this manner, we have saved approximately of the trees.
Solution 3
- A bill is mandatory because it’s the minimum and we cannot leave this.
- Moving towards , let’s add this to our money bills list too.
- We can skip as with the combination of and , we can get $3 so no need to print bill.
- We will be needing and after that, until , we won’t be needing any extra money bill as with the combination of , and we can manage the range .
- Now we will add to our bills which will allow us to perform any transaction from to .
- Similarly, we need to have a bill in order to widen our range to to .
- So we have the following money bills: , , , and . But we cannot add as this will exceed our limit of . As we can now do any transaction from to and the remaining amount is . Therefore, we’ll have the last bill worth . Now the question is how to pay the amount from . We know any quantity less than or equal to can be easily paid through . For any amount greater than , we will first pay through the bag, then the remaining amount () will be within the range of , which we already know how to pay through the other bags. E.g. if we want to pay the solution will be .
In this fashion, we can do transactions within our complete given range, and we have saved the trees as much as possible.
The final list of money bills will be .
This pattern is similar to our binary number system where each bit added to the right doubles the range.
Exercise
Suppose we have two strings that only have one known property – when lit at either end, it takes only hour to burn either of them. The rate at which the strings burn is random, and each string is different (hence if we break the string into two halves it may be the case that both strings have different burning durations). How can we measure hours and minutes?
Directions to look at the problem
To measure hours, first, we burn a string from one end, and when it is completely burned, an hour has passed. After that, we have to burn the second string from both ends simultaneously. Once it is completely burned, hours have passed. So, a total of hours have been measured.
For minutes, a similar strategy needs to be implemented.