Excercise 2-7.
In the following exercise we will compute the greatest common factor (GCF) and least common multiple of two integers.
  1. First, write a script that will ask the user for two integers and store them in two variables: Num1 and Num2.
  2. Now write an expression (but do not build it with Scratch blocks yet) that would return True if both Num1 and Num2 were evenly divisible by SomeFactor (i.e., SomeFactor is a common factor to both Num1 and Num2). Hint: Think about which operator deals with remainders (a remainder of 0 means evenly divisible). Then think about the logic statement that would return True if both are evenly divisible.
  3. We need to search only through numbers less than or equal to the smaller of the two integers that the user enters. Create two variables--MinNum and MaxNum--and write an if statement that compares Num1 and Num2 and appropriately places the smaller into MinNum and the larger into MaxNum.
  4. Now create a loop that will repeat MinNum number of times. Create a variable K and use it as a counter for each iteration of the loop.
  5. Create a variable called GCF. Create the expression containing mod from the previous problem and place it in an if statement that assigns the value of the factor to GCF if the if statement is True. Return a string that says "The GCF is " along with the value of the GCF. To test your script, use 18 and 12 (GCF = 6) as well as 697 and 731 (GCF = 17).
  6. Now we will change the script to output the least common multiple (LCM). Think carefully about how to do this. If you take the smaller of the two integers (MinNum) and start multiplying 2, 3, 4, etc times this integer, how can you determine if a multiple of MinNum is also a multiple of the larger integer (MaxNum)? Hint: If K is an integer multiplier, MinNum*K is an integer multiple of MinNum. How can we determine if MinNum*K is a common multiple of MaxNum?
  7. Use the result of the previous problem to write the script to determine the least common multiple. Keep in mind that you need to loop only far enough to find the first common multiple (because this will be the least common multiple). Thus, set a variable called LCM to 0. Loop until LCM is not zero. Change the if statement so that it uses the result of the previous problem. Test your code with 16 and 20 (LCM = 60) as well as 26 and 17 (LCM = 442).