CSE 670 Least Common Multiple LCM Processor Tom Slides: 5 Download presentation CSE 670 Least Common Multiple (LCM) Processor Tom Prohaszka 03/01/04 thp-Least Common Multiple Algorithm The LCM Algorithm LCM defined: The least common multiple of the positive integers num 1 and num 2 is the smallest integer that is divisible by both num 1 and num 2. //Version 1 -Iterative multipliation int LCM 1(unsigned int num 1, unsigned int num 2) { int LCM_Val, count 1, count 2, prod 1, prod 2; //Version 2 -Iterative subtraction int CLcm. View: : LCM 2(unsigned int num 1, unsigned int num 2) { int LCM_Val, count 1, count 2, prod 1, prod 2, product; product = num 2*num 1; prod 1 = product; for ( count 1 = num 2; count 1 >=1; count 1 --) { prod 1 = count 1*num 1; for(count 2 =num 1; count 2>=1; count 2 --) { prod 2 = count 2*num 2; if(prod 1 == prod 2) { LCM_Val = prod 1; } } } return LCM_Val; } for ( count 1 = num 2; count 1 >=1; count 1 --) { prod 2 =product; for(count 2 =num 1; count 2>=1; count 2 --) { if(prod 1 == prod 2) { LCM_Val = prod 1; } prod 2 = prod 2 -num 2; } prod 1 = prod 1 - num 1; } return LCM_Val; } 10/27/2021 thp-Least Common Multiple Algorithm 2 10/27/2021 thp-Least Common Multiple Algorithm 3 10/27/2021 thp-Least Common Multiple Algorithm 4 Example: LCM of 22 and 33 • • • • • • • • • 10/27/2021 Outer Loop Subtracts 22 from 726 704 682 660 638 616 594 572 550 528 506 484 462 440 418 396 374 352 330 308 286 264 242 220 198 176 154 132 110 88 66 44 22 LCM is: 66 • • • • • Inner Loop Subtracts 33 from 726 LCM: 726 693 … 33 Inner Loop 726 693 … Inner Loop Iterations… LCM: 660 LCM: 528 LCM: 462 …. LCM: 132 LCM: 66 … LCM is: 66 thp-Least Common Multiple Algorithm 5