00-Self Test 2. Prime pair conjecture (20)

Source: Internet
Author: User

Let's define DN as: Dn = Pn+1-pN, where Pi is the first prime. Obviously there is D1=1 and for n>1 thereis an even number of D N. "The prime number pairs conjecture" that there are infinitely many pairs of adjacent and the difference is 2 primes.

Now given any positive integer n (<5), calculate the number of prime pairs that do not exceed N to satisfy the conjecture.

input Format: each test input contains 1 test cases, giving a positive integer n.

output format: the output of each test case occupies one line, not exceeding N of the number of prime pairs that satisfy the conjecture.

Input Sample:
20
Sample output:

4

#include <iostream>  using namespace std;    BOOL IsPrime (unsigned long N)       //The prime number of the Web check method  {          if (n <= 3) {          return n > 1;          }       else if (n% 2 = = 0 | | n% 3 = = 0) {          return false;          }       else{for            (unsigned short i = 5; I * I <= n; i + = 6) {               if (n% i = = 0 | | n% (i + 2) = = 0) {                   return false;                   }                  }           return true;      }  }    int main ()  {      int i,j=0,n,c[100000],count=0;      cin>>n;      for (i=1;i<=n;i++) {          if (IsPrime (i))              c[j++]=i;      }      for (i=j-1;i>=1;i--) {          if ((c[i]-c[i-1)) ==2)              count++;      }      cout<<count<<endl;      return 0;  }
The following is written by oneself, not all points.

#include <iostream>

#include <cmath>
using namespace Std;
int main ()
{
int N;
cin>>n;
int a[100000];
a[0]=2,a[1]=3;
int k=2,num=0;
for (int i=3;i<=n;i++)
{
for (int j=2;j<=int (sqrt (i)); J + +)
{
if (i%j!=0)
{
A[k]=i;
k++;
}
}
}
for (int i=1;i<=k;i++)
{
if (a[i]-a[i-1]==2)
num++;
}
cout<<num<<endl;
return 0;
}

00-Self Test 2. Prime pair conjecture (20)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.