Algorithms for greatest common divisor and least common multiple

Source: Internet
Author: User
Tags greatest common divisor new set

Short division: #include <stdio.h> #include <stdlib.h>int max_approximate (int num1, int num2) {if  (num1 > num2) {int tmp = 0;tmp = num1;num1 = num2;num2  = tmp;} int min = num1;while  (min) {if  ((num2%min == 0) && (num1%min==0)) { Return min;} min--;} return 1;} Int main () {int num1 = 0;int num2 = 0;int ret = 0;scanf ("%d%d ",  &num1,&num2); Ret=max_approximate (num1, num2);p rintf ("%d\n ",  ret); System (" pause "); return 0;} The method of division: #include <stdio.h> #include <stdlib.h>int max_approximate (int num1, int  num2) {if  (num1 > num2) {int tmp = 0;tmp = num1;num1 = num2 ; num2 = tmp;} int r = num2%num1;while  (r != 0) {Num2 = num1;num1 = r;r &NBSP;=&NBSP;NUM2%NUM1;} Return num1;} Int main () {int num1 = 0;int num2 = 0;int ret = 0;scanf ("%d%d ",  &num1, &num2); Ret = max_approximate (num1, num2);p rintf ("%d\n ",  ret); System ("pause"); return 0;} More subtractive methods: #include <stdio.h> #include <stdlib.h>int max_approximate (int num1, int  num2) {if  (num1 > num2) {int tmp = 0;tmp = num1;num1 = num2 ; num2 = tmp;} int r = num2-num1;while  (R&NBSP;!=&NBSP;NUM1) {if  (num1>r) {num2 = num1; Num1 = r;} Else{num2 = r;} R&NBSP;=&NBSP;NUM2-NUM1;} RETURN&NBSP;NUM1;} Int main () {int num1 = 0;int num2 = 0;int ret = 0;scanf ("%d%d ",  &num1, &num2); Ret = max_approximate (num1, num2);p rintf ("%d\n ",  ret); System ("pause"); return 0;}

1. The Euclidean method

Algorithm: is to use a large number in addition to decimals, if the remainder is not 0, the remainder and the smaller number constitute a new set of numbers, continue the above division, know that the large number is the decimal, when the relatively small number is greatest common divisor

2. More subtractive methods:

The first step: arbitrarily given two positive integers, judging whether they are even. If so, use a 2 reduction, or the second step if not.

The second step: reduce the smaller number by a larger number, and then compare the resulting difference with the smaller number, and subtract the decimal number by the large numbers. Continue this operation until the resulting meiosis and difference are equal.

The product of several 2 and second intermediate numbers in the first step is the desired greatest common divisor.

The "equal number", which is said, is greatest common divisor. The method of seeking "equal number" is "more subtractive". So the subtraction method is also called the equivalence algorithm.

3. The difference between the Euclidean method and the more subtractive loss technique

(1) are the methods to find the maximum common factor, the calculation of the division-based method to divide the main, more subtractive loss of the main subtraction, the calculation of the number of times the calculation of the method is relatively small, especially when the number of two digital differences between the number of differences more obvious.

(2) from the manifestation of the results, the result of the method is that the dividing remainder is 0, and the subtraction is obtained by the same difference between the meiosis and the differential.

4. Seeking least common multiple

The product of the two numbers of greatest common divisor and least common multiple is the product of these two numbers, so the least common multiple can be converted to the maximum common factor.


This article is from the "11132019" blog, please be sure to keep this source http://11142019.blog.51cto.com/11132019/1752802

Algorithms for greatest common divisor and least common multiple

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.