PAT (Basic Level) Practise (中文)1007. 素数对猜想 (20)

1007. 素数对猜想 (20)

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

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

输入格式:每个测试输入包含1个测试用例,给出正整数N。

输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。

输入样例:
20
输出样例:
4
//PAT (Basic Level) Practise (中文)
//1007. 素数对猜想 
//create by zlc on 12/16/2014
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int judge_prime();

int main()
{
    int N,i,j,cnt=0,front,mask=0;
    scanf("%d",&N);
    for(i=1;i<=N;i++)
    {
        mask=judge_prime(i);
        if(mask)
        {
            mask=0;
            if(i+2<=N)
            {
                mask=judge_prime(i+2);
                if(mask)
                    cnt++;
            }

        }
    }
    printf("%d\n",cnt);
    return 0;
}

int judge_prime(int n)              // is or not Prime 
{
    int i,isPrime=1;
    if(n==1||(n%2==0&&n!=2))    //n=1 或者 n为不等于2的偶数
        isPrime=0;
    for(i=3;i<=sqrt(n);i+=2)
    {
        if(n%i==0)
        {
            isPrime=0;
            break;
        }
    }
    return isPrime;
}

  

posted @ 2014-12-16 16:25  笨笨小成  阅读(118)  评论(0编辑  收藏  举报