In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Java to find the maximum common divisor". In daily operation, I believe many people have doubts about how to use Java to find the maximum common divisor. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Java to find the maximum common divisor". Next, please follow the editor to study!
/ * * @ date 11:33 on 2019-7-25 * description: find the simplest idea of finding the greatest common divisor * / public class CommonDivisor {/ * first edition * find half of the smaller number, from large to small, and start trying to find out that you can divide the maximum number by two numbers at the same time * this method is violent enumeration. Will cycle many times * * @ param a * @ param b * @ return * / public static int getGreatestCommonDivisor (int a, int b) {int big = a > b? A: b; int small = a
< b ? a : b; if (big % small == 0) { return small; } for (int i = small / 2; i >1; return -) {if (small% I = = 0 & & big% I = = 0) {return I;}} return 1 } / * * second version * Euclid algorithm: to find the maximum common divisor * two positive numbers an and b (a > b) Their greatest common divisor is equal to the remainder c and b divided by an and b. We can use the recursive method to simplify the problem. * the greatest common divisor of eg 10 and 25 is equal to the maximum common divisor of the remainder 5 and 10. 5 * disadvantage: when the two numbers are large, the conversion efficiency of a% b is low * * @ param a * @ param b * @ Return * / public static int getGreatestCommonDivisor2 (int a Int b) {int big = a > b? A: b; int small = a
< b ? a : b; if (big % small == 0) { return small; } return getGreatestCommonDivisor2(big % small, small); } /** * 第三版本 * 九章算术 * 更相减损术 :两个正整数a,b(a>B) their greatest common divisor is equal to the difference c of a murb and the greatest common divisor of the smaller number b * disadvantages: when the difference between the two numbers is very large, the number of recursion is too large * * @ param a * @ param b * @ return * / public static int getGreatestCommonDivisor3 (int a, int b) {if (a = = b) {return a } int big = a > b? A: b; int small = a
< b ? a : b; return getGreatestCommonDivisor3(big - small, small); } /** * 第四版本 * 九章算术 更相减损术和辗转相除法结合起来, 更相减损术上使用位移操作 * 更相减损术 :两个正整数a,b(a>B) their greatest common divisor is equal to the difference c of a murb and the greatest common divisor of the smaller number b * disadvantages: when the difference between the two numbers is very large, the number of recursion is too large * * @ param a * @ param b * @ return * / public static int getGreatestCommonDivisor4 (int a, int b) {if (a = = b) {return a } if ((a & 1) = = 0 & & (b & 1) = = 0) {return getGreatestCommonDivisor4 (a > > 1, b > 1) > 1, b);} else if ((a & 1)! = 0 & & (b & 1) = = 0) {return getGreatestCommonDivisor4 (a, b > 1);} else {int big = a > b? A: b; int small = a < b? A: B; return getGreatestCommonDivisor4 (big-small, small);}} public static void main (String [] args) {System.out.println (getGreatestCommonDivisor4 (25,5)); System.out.println (getGreatestCommonDivisor4 (100,80)); System.out.println (getGreatestCommonDivisor4 (27,14)) }} at this point, the study on "how to use Java to find the greatest common divisor" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.