一、案例工程
1、案例代码
- #include <stdio.h>
- #include <stdbool.h>
- #include <math.h>
- #define YES 1
- #define NO 0
- int isprime(int x)
- {
- int i = 2;
- bool flag = false;
- for(; i < sqrt(x); i++)
- {
- if(x % i == 0)
- {
- flag = false;
- return NO;
- }
- else
- flag = true;
- }
- return YES;
- }
- void prove_1_even_has_2_prime(int x)
- {
- int i = 2;
- for (; i < x/2; i++)
- {
- if(isprime(i))
- {
- if(isprime(x-i))
- printf("%d is a even,the prime are : %d,%d\n",x,i,x-i);
- }
- }
- }
- int main()
- {
- int a;
- printf("Input one even :\n");
- scanf("%d",&a);
- if(a%2 == 0)
- prove_1_even_has_2_prime(a);
- else
- printf("%d is not a ever number\n",a);
- return 0;
- }
2、编译过程
- [root@localhost arithmetic]# gcc -o evenAndprime evenAndprime.c -lm
注意要加上-lm
3、实数验证
- [root@localhost arithmetic]# ./evenAndprime
- Input one even :
- 50
- 50 is a even,the prime are : 3,47
- 50 is a even,the prime are : 7,43
- 50 is a even,the prime are : 9,41
- 50 is a even,the prime are : 13,37
- 50 is a even,the prime are : 19,31
二、算法分析
算法思想:如果需验证的数置于变量X中,则可依次从X中减去i,其中i从2变化到X/2。
阅读(2614) | 评论(0) | 转发(0) |