利用C語言驗證哥德巴赫猜想

ZZ100861122發表於2020-12-26

本程式利用C語言驗證哥德巴赫猜想
程式碼如下

#include<stdio.h>
#include<math.h>
void spilt_eve(unsigned int array[3]);
void spilt_odd(unsigned int array[4]);
int isprime(int x);
void error_1(int a);
void main()
{
unsigned int a = 1, c = 1;
unsigned int eve[3] = { 0 };
unsigned int odd[4] = { 0 };
//輸入數字
for (; a != 0;)
{
printf(“Please input a number.\n(from 0 to 65535)\n”);//最好不要輸入超過65535的數字,否則會讓本程式執行時間過長
scanf_s("%d", &a);
if ((a % 2 == 0) && (a < 6))
{
printf(“Error Begin.\nif the number is even number, please make sure that it is beyond 6.\n”);
error_1(1);
continue;
}
else if ((a % 2 != 0) && (a < 9))
{
printf(“Error Begin.\nif the number is odd number, please make sure that it’s beyond 9.\n”);
error_1(1);
continue;
}

    //將數字進行分類,分出奇數和偶數
    if (a % 2 == 0)
    {
        eve[0] = a;
        spilt_eve(eve);

    }

    else
    {
        odd[0] = a;
        spilt_odd(odd);

    }


}

}

//確保輸出的數字是質數
int isprime(int x)
{
int i, j = 1;
for (i = 2; i <= (int)sqrt(x); i++)
{
if (x % i == 0)
{
j = 0;
break;
}
}
return(j);
}

void error_1(int a)
{
printf(“請選擇一個合適的數字”);
}

//偶數
void spilt_eve(unsigned int eve[3])
{
int i, j;
i = eve[0];
for (j = 3; j <= i / 2; j++)
{
if (isprime(j) == 1)
{
if ((isprime(i - j == 1)))
{
eve[1] = j;
eve[2] = i - j;
printf("%d=%d+%d\n", eve[0], eve[1], eve[2]);
}
}
}
}

//奇數
void spilt_odd(unsigned int odd[4])
{
unsigned int eve[3];
int i, j, k;
i = odd[0];
for (k = 3; k <= i / 3; k++)
{
if (isprime(k) == 1)
{
for (j = k; j <= i / 2; j++)
{
if (isprime(j) == 1)
{
if (isprime(i - k - j) == 1 && ((i - k - j) >= j))
{

                    odd[1] = k;
                    odd[2] = j;
                    odd[3] = i - k - j;
                    printf("%d=%d+%d+%d\n", odd[0], odd[1], odd[2], odd[3]);
                }
            }
        }
    }
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

warning:::如果你們不怕電腦CPU燒掉,輸入10億以內的數字還是可以的

相關文章