求素数并验证哥德巴赫猜想

 1 //素数的求解
 2 package sdau_zjj;
 3 
 4 public class sushu {
 5        private int x;
 6 
 7   public sushu(int x) {
 8     this.x = x;
 9   }
10     public int pd() {
11       int f=0;
12       int i=2;
13       while(i<this.x&&x%i!=0) {
14         ++i;
15       }
16       if(i==this.x) {
17         f=1;
18       }
19       return f;
20     }
21 }
22 
23 
24 package sdau_zjj;
25 
26 public class sushutest {
27 
28   public static void main(String[] args) {
29     String jg = "";
30     int m = 0;
31     for (int i = 1; i <= 100; ++i) {
32       sushu a = new sushu(i);
33       if (a.pd() == 1) {
34         ++m;
35         jg = jg + i + " ";
36         if (m % 10 == 0) {
37           jg = jg + "\r\n";
38         }
39       }
40     }
41     System.out.println(jg);
42        
43     System.out.println("验证哥德巴赫猜想");
44     int n = 100;
45     for (int a = 2; a <= n - 1; ++a) {
46         int b=n-a;
47         sushu x=new sushu(a);
48         sushu y=new sushu(b);
49         if(x.pd()==1&&y.pd()==1) {
50           System.out.println(a+"+"+b+"+"+"="+m);
51         }
52     }
53   }
54 
55 }

 

posted @ 2020-11-22 09:36  丁帅帅dss  阅读(112)  评论(0)    收藏  举报