1007. Prime Pair Conjecture

Let us define dn as: dn = pn+1 - pn, where pi is the ith prime number. Obviously d1=1 and dn is even for n>1. The "prime pair conjecture" states that "there are infinitely many pairs of adjacent prime numbers that differ by 2". Now given any positive integer N (< 105), please count the number of prime pairs that satisfy the conjecture not exceeding N.
Input format:
Each test input contains 1 test case, giving a positive integer N.
Output format:
The output of each test case occupies one line, and does not exceed the number of N prime pairs that satisfy the conjecture.
Input sample:
20
Output sample:
4
Note: The title is very simple, but there is still a small error when doing it. That is, when using sqrt(n), it should actually be <=, otherwise 4 will also be judged as a prime number.

#include<stdio.h>
#include<math.h>
int sushu(int i)
{
    int n;
    for (n=2; n<=sqrt(i); n++)
        if (i%n==0) return 0;
    return 1;
}
int main()
{
    int a[10000];
    int i, j, n;
    scanf("%d", &n);
    for (i=2,j=0; i<=n; i++){
        if (sushu(i)==1) a[j++] = i;
    }
    int count = 0;
    for (i=1; i<j; i++){
        if (a[i]-a[i-1]==2) count++;
    }
    printf("%d", count);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325584383&siteId=291194637