Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to find the maximum common divisor in C language

Shulou Source: shulou.com Published: 2022-06-02 08:12:53 10月03日 Update

In this issue, the editor will bring you about how to find the maximum common divisor in C language. the article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Case solving

If you need to ask for the greatest common divisor of two positive integers, using the Euclidean algorithm, it goes like this:

1997 / 615 = 3 (remaining 152)

615 / 152 = 4 (remaining 7)

152 / 7 = 21 (remaining 5)

7 / 5 = 1 (remaining 2)

5 / 2 = 2 (remaining 1)

2 / 1 = 2 (remainder 0)

So far, the maximum common divisor is 1.

The division operation is done repeatedly with the divisor and the remainder. When the remainder is 0, the divisor of the current formula is taken as the maximum common divisor, so the maximum common divisor of 1997 and 615 is obtained.

Novice version

# include # include int result (int m, int n) {int r; if (m > n) {r = m, m = n, n = r;} r = n% m; while (r! = 0) {n = m; m = r; r = n% m;} return m;} int main () {

Printf ("result:%d\ n", result (12,9)); return 0;}

This way of writing is a very simple idea.

1. First find the maximum of the two.

two。 Then use a loop to describe tossing and turning and divide.

Big shot version

# include

# include int result (int m, int n) {return n? Result (n, m% n): n;} int main () {printf ("result:%d\ n", result (12, 9)); return 0;} this is how to find the maximum common divisor in the C language shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Maximum common divisor maximum common divisor language remainder content divisor analysis major two small and medium rich in content writing that is ideas integers articles novices more maximum Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Redmi MySQL vpn