In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian to share with you how to calculate the greatest common divisor and least common multiple of two numbers in C language, I believe most people still don't know how to understand, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's find out together!
If there is a natural number a divisible by a natural number b, then a is called a multiple of b and b is a divisor of a. The greatest common divisor is the largest common divisor of two or more integers. The least common multiple is the lowest common multiple of two or more integers except zero.
Calculate the greatest common divisor of two numbers
According to the definition of divisors, all divisors of a certain number must not be greater than the number itself, and the greatest common divisor of several natural numbers must not be greater than any of them. The greatest common divisor of any two positive integers is to find the greatest natural number that is not greater than either of the two, but can divide two integers exactly at the same time.
Algorithm ideas: in accordance with the order from large (two integers in the smaller number) to small (to the smallest integer 1) to find the first can divide two integers at the same time the natural number, that is, for seeking.
Code example:
#includeint main(){ int m, n, temp, i; printf("Please enter any 2 numbers:\n"); scanf("%d%d", &m, &n); if(m0; i--) /* Find the natural numbers satisfying the condition in descending order */ if(m%i==0 && n%i==0) {/* Output the natural number that satisfies the condition and end the loop */ printf("greatest common divisor of %d and %d is: %d\n", m, n, i); break; } return 0;}
Output:
Find the least common multiple of two numbers
Idea: Find the least common multiple of any two positive integers, that is, find the smallest natural number that can be divisible by two integers at the same time.
Code example:
#includeint main(){ int m, n, temp, i; printf("Please enter any 2 numbers:\n"); scanf("%d%d", &m, &n); if(m0; i++) /* Start with large numbers to find natural numbers that satisfy the condition */ if(i%m==0 && i%n==0) {/* Output the natural number that satisfies the condition and end the loop */ printf("least common multiple of %d and %d is: %d\n", m, n, i); break; } return 0;}
Output:
The least common multiple can also be found using the greatest common divisor, formula:
● Least common multiple = product of two numbers/maximum common denominator
The above is "C language how to calculate the greatest common divisor and least common multiple of two numbers" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.