素数(质数)4
来源:互联网 发布:北京和隆优化科技 编辑:程序博客网 时间:2024/05/19 03:22
/*
题目1440:Goldbach's Conjecture
题目描述:
Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2.
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.
A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
输入:
An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.
输出:
Each output line should contain an integer number. No other characters should appear in the output.
样例输入:
6
10
12
0
样例输出:
1
2
1
*/
#include <stdio.h>
#define max 100000
int prime[max+1];
int primesize;
bool mark[max+1];
void init(){
int i,j;
for(i=1;i<max;i++)
mark[i] = false;
primesize = 0;
for(i=2;i<max;i++){
if(mark[i] == false){
prime[primesize++]=i;
for(j=2*i;j<max;j += i)
mark[j] = true;
}
}
}
int main(){
int n;
init();
while(scanf("%d",&n) != EOF && n != 0){
int i,j,count=0;
for(i=0;prime[i]<n;i++)
for(j=i;prime[j]<n;j++)
if(prime[i]+prime[j] == n)
count++;
printf("%d\n",count);
}
return 0;
}
题目1440:Goldbach's Conjecture
题目描述:
Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2.
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.
A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
输入:
An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.
输出:
Each output line should contain an integer number. No other characters should appear in the output.
样例输入:
6
10
12
0
样例输出:
1
2
1
*/
#include <stdio.h>
#define max 100000
int prime[max+1];
int primesize;
bool mark[max+1];
void init(){
int i,j;
for(i=1;i<max;i++)
mark[i] = false;
primesize = 0;
for(i=2;i<max;i++){
if(mark[i] == false){
prime[primesize++]=i;
for(j=2*i;j<max;j += i)
mark[j] = true;
}
}
}
int main(){
int n;
init();
while(scanf("%d",&n) != EOF && n != 0){
int i,j,count=0;
for(i=0;prime[i]<n;i++)
for(j=i;prime[j]<n;j++)
if(prime[i]+prime[j] == n)
count++;
printf("%d\n",count);
}
return 0;
}
0 0
- 素数(质数)4
- 打印质数(素数)
- 求质数(素数)
- 素数(质数) 1
- 素数(质数) 2
- 素数(质数) 3
- 判断素数(质数)
- 素数(质数)
- 判断素数(质数)
- 判素数(质数)
- 素数(质数)问题
- 找素数(质数)算法
- 质数(素数)判定算法
- C#找质数(素数)
- 求素数(质数)算法
- 求范围内质数(素数)
- 素数(质数)判断方法
- 质数 素数
- RedirectResult/RedirectToRouteResult类型
- android webview 篇
- ListView下拉刷新,上拉加载
- [Spring实战系列](9)装配集合
- linux下c语言编程,使用socket发送文件和数据
- 素数(质数)4
- vim使用教程(转自菜鸟教程)
- sdk(安卓)
- 反汇编---汇编基础学习
- 判断语言环境
- 学习笔记3 Supervised Neural Networks
- Android 三大图片缓存原理、特性对比
- 学习笔记------数据结构(C语言版)栈应用 表达式求值
- 大数据IMF行动传奇 迈向SparkContext天堂之门! 探索天堂之门!