60 likes | 221 Views
Lab02. Greatest Common Divisor Problem Short for GCD. Lab Description:. Given two natural numbers m and n, find the GCD of them. Definition 1: the GCD of m and n is the largest natural number that exactly divides both of them; i.e. it does not leave remainder. Find . Algorithm for GCD.
E N D
Lab02 Greatest Common Divisor Problem Short for GCD
Lab Description: • Given two natural numbers m and n, find the GCD of them. • Definition 1: the GCD of m and n is the largest natural number that exactly divides both of them; i.e. it does not leave remainder. Find
Algorithm for GCD • Step 1: Find the prime factors of m. • Step 2: Find the prime factors of n. • Step 3: Identify all the common factors in the two prime expressions found in Steps 1 and 2. (If p is a common factor occurring pm and pn times in m and n, respectively, it should be repeated min(pm,pn) times). • Step 4: Compute the product of all the common factors and return it as the greatest common divisor of the numbers given.
An Example • Input: m = 330, n = 18 • Step 1: • Find the prime factors of 330 • 330 = 2 * 3 * 5 * 11 • Step 2: • Find the prime factors of 18 • 18 = 2 * 3* 3
Step 3: • Identify all the common factors in the two prime expressions found in Steps 1 and 2. • 330 = 2 * 3 * 5 * 11 • 18 = 2 * 3* 3 • 2 is a common factor occurring 1 time in 330 and 18, so 2 is what we need for the GCD. • 3 is a common factor occurring 1 time in 330 and 2 times in 18, so min(1,2) = 1 is what we need for the GCD. • Step 4: • Compute the product of all the common factors and return it as the greatest common divisor of the numbers given. • GCD = 2*3=6. Return 6
Program Requirement and Test Program • http://my.fit.edu/~qsong2008/labs/lab02.html