Designing Different Algorithms

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, 1,2,3,4,6,8,121, 2, 3, 4, 6, 8, 12 and 2424 are the factors of 2424. Factors can be very useful if they are common, mainly in simplifying fractions.

GCD or the highest common factor is the largest factor among the factors of the two numbers.

1212 is the largest common factor for 2424 and 36,36, hence, GCD(24,36)=12.\text{GCD}(24, 36) = 12.

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 55-liter and 88-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 1,2,3,4,6,or 71, 2, 3, 4, 6, or \space 7, 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 8l8l container and pour that into the smaller one with 5l5l capacity. As a result, only 33 liters will be left in the 8l8l container.
  • Empty the 5l5l container and pour the remaining 33 liters from the 8l8l container into it.
  • Now, fill the 8l8l container again, and pour that into the smaller one that already contains 33 liters. As a result, 66 liters will be left in an 8l8l container.
  • We have to empty the 5l5l container and pour the 8l8l container, which already has 66 liters, into it. This procedure will leave 11 liter lemonade in the 8l8l container. We can give it to the customer.

2 liters

  • Fill the 5l5l container and pour it into the larger container with 8l8l capacity. Now 33 more liters can be added to the larger container.
  • Fill the 5l5l container again and pout into the 8l8l container where the available empty capacity is 55 liters. As result, 22 liters will be left in a 5l5l container, which can be forwarded to the customer.
Please login to launch live app!
Question

Can we do the same if we had 6-liter and 9-liter containers?

Show Answer

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 22 as it only contains two alphabets, 11 and 00, to represent any number.

The binary number system starts with 00, and the next number can be obtained by adding 11 to it. So, the 2nd number will be 11. For the next number, (1+1=10)2(1+1 = 10)_2, we keep 00 to the right and carry 11 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 $1\$1 and $50\$50 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 5050 money bills of $1\$1 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 2424 money bills of $2\$2 each and 22 money bills of $1\$1 each, we can achieve our goal. In this manner, we have saved approximately 5050% of the trees.

Solution 3

  • A $1\$1 bill is mandatory because it’s the minimum and we cannot leave this.
  • Moving towards $2\$2, let’s add this to our money bills list too.
  • We can skip $3\$3 as with the combination of $1\$1 and $2\$2, we can get $3 so no need to print $3\$3 bill.
  • We will be needing $4\$4 and after that, until $7\$7, we won’t be needing any extra money bill as with the combination of $1\$1, $2\$2 and $4\$4 we can manage the range 171-7.
  • Now we will add $8\$8 to our bills which will allow us to perform any transaction from $1\$1 to $15\$15.
  • Similarly, we need to have a $16\$16 bill in order to widen our range to $1\$1 to $31\$31.
  • So we have the following money bills: $1\$1, $2\$2, $4\$4, $8\$8 and $16\$16. But we cannot add $32\$32 as this will exceed our limit of $50\$50. As we can now do any transaction from $1\$1 to $31\$31 and the remaining amount is 5031=1950-31=19. Therefore, we’ll have the last bill worth $19\$19. Now the question is how to pay the amount from 32...5032 ... 50. We know any quantity less than or equal to 3131 can be easily paid through $[1,2,4,8,16]\$[1,2,4,8,16]. For any amount AA greater than 3131, we will first pay through the $19\$19 bag, then the remaining amount (A19A-19) will be within the range of 1...311...31, which we already know how to pay through the other bags. E.g. if we want to pay $40\$40 the solution will be 19+16+4+1=4019+16+4+1=40.

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 $[1,2,4,8,16,19]\$[1, 2, 4, 8, 16, 19].
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 11 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 1.51.5 hours and 4545 minutes?

Directions to look at the problem

To measure 1.51.5 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, 0.50.5 hours have passed. So, a total of 1.51.5 hours have been measured.

For 4545 minutes, a similar strategy needs to be implemented.