1007. 素数对猜想 (20)
来源:互联网 发布:外国域名注册网站 编辑:程序博客网 时间:2024/04/27 23:12
让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数。显然有 d1=1 且对于n>1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N (< 105),请计算不超过N的满足猜想的素数对的个数。
输入格式:每个测试输入包含1个测试用例,给出正整数N。
输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。
输入样例:20输出样例:
4
import java.util.Scanner;public class Main {public static void main(String args[]) {int[] num = new int[100001];Scanner cin = new Scanner(System.in);while (cin.hasNext()) {int n = cin.nextInt();num[0] = 2;num[1] = 3;int count = 2;int temp;for (int i = 4; i <= n; i++) {temp = 0;for (int j = 2; j <= Math.sqrt(i); j++) {if (i % j == 0) {temp = 1;break;}}if (temp != 1) {num[count++] = i;}}temp = 0;for (int i = 0; i < count - 1; i++) {if (num[i + 1] - num[i] == 2) {temp++;}}System.out.println(temp);}}}#include <iostream>#include <math.h>using namespace std;int main() { int i, j; int num[100001]; int n; int count = 0; int temp = 0; while(cin>>n) { num[0] = 2; num[1] = 3; count = 2; for( i = 4; i <= n; ++i ) { temp = 0; for( j = 2; j <= sqrt(i); ++j ) { if( (i % j) == 0 ) { temp = 1; break; } } if( temp != 1 ) { num[count++] = i; } } temp = 0; for( i = 0; i < count - 1; ++i ) { if( (num[i+1] - num[i]) == 2 ) ++temp; } cout<<temp<<endl; } return 0;}
0 0
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 1007. 素数对猜想 (20)
- 乘积的末三位
- blog
- include注意点
- 算法竞赛入门经典第3章【小结与习题】
- 将秒数转换成NSDate,加上时区偏移
- 1007. 素数对猜想 (20)
- HttpRuntime.Cache缓存数据
- jsp乱码解决大全
- ios 获取当前时间汇总
- 虚拟化–XenServer中Windows 7与XP多vCPU支持配置
- javascript对象
- 测试备库应用主库日志时有无using current logfile选项的区别
- Java多线程学习笔记
- java初学感谢四