日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

求两个数的最大公约数的3种办法

發布時間:2023/12/31 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 求两个数的最大公约数的3种办法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

import org.junit.Test;import java.util.ArrayList; import java.util.List;/*** 求兩個數的最大公約數* @author shadowolf* @date 2018/10/24 12:25*/ public class GreatestCommonDivisor {@Testpublic void greatestCommonDivisor() throws Exception {int result1 = GreatestCommonDivisor.getGreatestComonDivisor1(24, 60);System.out.println("result1: " + result1);int result2 = GreatestCommonDivisor.getGreatestComonDivisor2(24, 60);System.out.println("result2: " + result2);int result3 = GreatestCommonDivisor.getGreatestComonDivisor3(24, 60);System.out.println("result3: " + result3);}/*** 歐幾里得算法*/public static int getGreatestComonDivisor1(int num1, int num2) throws Exception {if (num1 < 1 || num2 < 1) {throw new Exception("params error !");}if (num1 < num2) {int temp = num1; num1 = num2; num2 = temp;}int r = num1 % num2;while (r != 0) {num1 = num2;num2 = r;r = num1 % num2;}return num2;}/*** 連續整數檢測算法*/public static int getGreatestComonDivisor2(int num1, int num2) throws Exception {if (num1 < 1 || num2 < 1) {throw new Exception("params error !");}if (num1 < num2) {int temp = num1; num1 = num2; num2 = temp;}if (num1 % num2 == 0) {return num2;}int t = num2 - 1;while (num1 % t != 0 || num2 % t != 0) {t--;}return t;}/*** 質因數算法*/public static int getGreatestComonDivisor3(int num1, int num2) throws Exception {if (num1 < 1 || num2 < 1) {throw new Exception("params error !");}if (num1 < num2) {int temp = num1; num1 = num2; num2 = temp;}// 求出2-n的所有質數序列List<Integer> primeNumList = getToNPrimeNumber(num1);List<Integer> num1PrimeList = getNAllPrimeNumber(num1, primeNumList);List<Integer> num2PrimeList = getNAllPrimeNumber(num2, primeNumList);List<Integer> commonList = getTwoLCommonList(num1PrimeList, num2PrimeList);int result = 1;for (Integer l : commonList) {result *= l;}return result;}/*** 求2-n中的質數序列* 使用算法: 埃拉托色尼篩選法*/public static List<Integer> getToNPrimeNumber(int n) {List<Integer> primeNumList = new ArrayList<>();int[] nums = new int[n+1];for (int p = 2; p <= n; p++) {nums[p] = p;}for (int p = 2; p < Math.floor(Math.sqrt(n)); p++) {int nn = p * p;while (nn <= n) {nums[nn] = 0;nn += p;}}for (int p = 2; p <= n; p++) {if (nums[p] != 0) {primeNumList.add(nums[p]);}}return primeNumList;}/*** 求n的所有質因數序列*/public static List<Integer> getNAllPrimeNumber(Integer num, List<Integer> primeNumList) {List<Integer> numPrimeList = new ArrayList<>();int i = 0;while (i < primeNumList.size() && num != 1) {if (num % primeNumList.get(i) == 0) {numPrimeList.add(primeNumList.get(i));num /= primeNumList.get(i);i = 0;continue;}i++;}return numPrimeList;}/*** 求兩個序列的共有序列*/public static List<Integer> getTwoLCommonList(List<Integer> list1, List<Integer> list2) {List<Integer> commonList = new ArrayList<>();for (Integer l : list1) {if (list2.contains(l)) {commonList.add(l);}}return commonList;}}

轉載于:https://my.oschina.net/shadowolf/blog/2252066

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的求两个数的最大公约数的3种办法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。