1 / 6

Lab02

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.

hao
Download Presentation

Lab02

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab02 Greatest Common Divisor Problem Short for GCD

  2. 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

  3. 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.

  4. 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

  5. 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

  6. Program Requirement and Test Program • http://my.fit.edu/~qsong2008/labs/lab02.html

More Related