1007 素数对猜想

让我们定义dn​为:dn​=pn+1​−pn​,其中pi​是第i个素数。显然有d1​=1,且对于n>1有dn​是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。

现给定任意正整数N(<105),请计算不超过N的满足猜想的素数对的个数。

输入格式:

输入在一行给出正整数N

输出格式:

在一行中输出不超过N的满足猜想的素数对的个数。

输入样例:

20

输出样例:

4
方法一:

#include<iostream>
using namespace std;
#include<stdio.h>
#include<string.h>
#include<math.h>
int sushu(int n){
    int temp = sqrt(n);
    for(int i=2;i<=temp;i++){
        if(n%i == 0){
            return 0;
        }
    }
    return 1;
}
int main(){
    int n;
    cin>>n;
    int a[10000];
    int count=0;
    int i=0;
    while(n>0)
    {
        if(sushu(n))
        {
            a[i++]=n;
        }
        n--;
    }
    for(int j=0;j<i-1;j++){
        if(a[j]-2==a[j+1]){
            count++;
        }
    }
    cout<<count<<endl;
    return 0;
}

方法二:

#include<iostream>
using namespace std;
#include<stdio.h>
#include<string.h>
#include<math.h>
int sushu(int n){
    int temp = sqrt(n);
    for(int i=2;i<=temp;i++){
        if(n%i == 0){
            return 0;
        }
    }
    return 1;
}
int main(){
    int n;
    cin>>n;
    int flag1 = 0,flag2 = 0,count = 0;
    for(int i=2;i<=n-2;i++){
        flag1 = sushu(i);
        flag2 = sushu(i+2);
        if(flag1&&flag2){
            count++;
        }
    }
    printf("%d",count);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

往~

感谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值