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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

验证哥巴赫猜想

發(fā)布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 验证哥巴赫猜想 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目:算法時間性能的經驗分析(驗證哥巴赫猜想)
驗證哥德巴赫猜想。統(tǒng)計其關鍵語句的執(zhí)行次數(shù)。并繪制規(guī)模-執(zhí)行次數(shù)散點圖。
(Goldbach Conjecture)猜想:即任一大于2的偶數(shù)都可寫成兩個質數(shù)之和。請驗證這個對于較大的偶數(shù)都是成立的。
算法:goldbach(n)
描述∶算法驗證對于小于等于n的偶數(shù),歌德巴赫猜想都是成立的。注意,goldbach(200)并非驗證200可以拆分成兩個質數(shù)的和,而是驗證對于所有≤n的偶數(shù),都可以拆分成兩個質數(shù)的和。
輸入:整數(shù)n
輸出:1表示成立;0表示猜想有誤
注1:用excel繪制散點圖,excel可以打開csv格式的文件。
注2∶csv(comma seperated values)是如下格式的文件。
文件位置:

public class Goldbach {/*** @param args* @throws IOException*/public static int cnt;public static List list = new ArrayList();public static int ARR_SIZE = 2500;public static int[] arr = new int[ARR_SIZE];public static void main(String[] args) throws IOException {Writer file = new FileWriter("diagram.csv");file.write("n,times\n");String line;cnt = 0;primes();for (int n = 200; n <= 5000; n += 50) {int temp = cnt;cnt = 0;if(n == 200){cnt += temp;}if (passGoldConj(n)) {line = Integer.toString(n) + "," + Integer.toString(cnt) + "\n";file.write(line);} else {System.out.println("Goldbach conjuction doesn't hold in our test");break;}}file.close();}public static boolean isPrime(int n) {if (list.indexOf(n) != -1){return true;}return false;}public static boolean passGoldConj(int n) {int k = 0;for (int i = 4; i <= n; i += 2 ){cnt++;for(int j = 2; j < i; j++) {cnt++;if (isPrime(j) && isPrime(i - j)) {k++;break;}}}if (k == (n/2 - 1)) {return true;}return false;}//這里使用的是篩選法求質數(shù)因子public static void primes(){arr[0] = 2;for (int i = 1, j = 3; i < ARR_SIZE; i++,j += 2){cnt++;arr[i] = j;}for (int i = 1; i < ARR_SIZE; i++) {cnt++;if (Math.pow(arr[i], 2) <= ARR_SIZE*2 && arr[i] != 0){int j, temp;temp = arr[i];//相比日常的篩選法,改進了一下for (j = temp * temp; j <= ARR_SIZE*2; j += 2*temp) {cnt++;int index = (j-1)/2;arr[index] = 0;}}}for (int i = 0; i < ARR_SIZE; i++) {cnt++;if (arr[i] != 0) {list.add(arr[i]);}} // System.out.println(list); // System.out.println(list.size());} }


時間復雜度是近乎O(n),線性的,但是求第一個質數(shù)是很廢時間的,因為我是將某數(shù)以內的所需質數(shù)全部求出。

純屬記錄分享,會有更好的方法

總結

以上是生活随笔為你收集整理的验证哥巴赫猜想的全部內容,希望文章能夠幫你解決所遇到的問題。

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