验证哥德巴赫猜想-C
2011-08-10 03:57
246 查看
/* * @author: Shang * @version: 0.01 * @date: 2011/08/11 * @src: C语言精彩编程百例(温海 张友 童伟 等编著,中国水利水电出版社) * @env: RHEL 5, gcc (GCC) 3.4.6 20060404 * @status: successful * @description: Break an integer into two parts. If both of the two parts are * prime and their sum is equal to the inputed integer, then print * integer = part1 + part2. * * @result: $ gcc Goldbach.c -std=c99 -lm $ ./a.out Input a positive and even integer: 39 The inputed integer is not right. Please try again: 28 28 = 5 + 23. * * @history: * 1. date: 2011/08/11 * author: shang * modification: Review the book */ #include <stdio.h> #include <math.h> #include <stdbool.h> bool isPrime(int num); int main() { int num = 1; printf("Input a positive and even integer: "); while(((num % 2) != 0) || num < 4) { if(num != 1) printf("The inputed integer is not right. Please try again: "); scanf("%d", &num); } for(int part1 = 2; part1 <= num / 2; part1++) { int part2 = num - part1; if(isPrime(part1) && isPrime(part2)) { printf("%d = %d + %d.\n", num, part1, part2); break; } } } bool isPrime(int num) { for(int i = 2; i <= (int) sqrt(num); i++) if(num % i == 0) return false; return true; }
相关文章推荐
- 验证哥德巴赫猜想(函数)
- 验证哥德巴赫猜想
- 验证“哥德巴赫猜想”/水仙花数/给定平面上任意三个点的坐标(x1,y1)、(x2,y2)、(x3,y3),检验它们能否构成三角形
- 初步验证哥德巴赫猜想(即:任意一个大于4的偶数,可以分解成2个质数之和*10000以内)
- 循环-04. 验证“哥德巴赫猜想”
- 循环-04. 验证“哥德巴赫猜想”(20)
- 使用函数验证哥德巴赫猜想
- 循环-04. 验证“哥德巴赫猜想”(20)
- 6-2 使用函数验证哥德巴赫猜想(20 分)
- 验证哥德巴赫猜想
- 用C语言来验证哥德巴赫猜想(定义的是int型)
- C语言验证哥德巴赫猜想
- 函数调用验证哥德巴赫猜想
- OJ题:验证哥德巴赫猜想(C++)
- 1093: 验证哥德巴赫猜想(函数专题)
- 40.验证哥德巴赫猜想:一个大于2的偶数总可以分解成两个素数的和
- zzuli OJ 1093: 验证哥德巴赫猜想(函数专题)
- Algorithm JAVA写算法 验证哥德巴赫猜想
- Java编程实现验证哥德巴赫猜想
- 编程验证哥德巴赫猜想