Conjecture of pat_1007 primes

Source: Internet
Author: User

Today I think more of the problem is not a come out now. Let's update the topic of the afternoon. It's almost 0 o'clock. Helpless Ah.

Problem Description:

Let's define the DN as: DN = PN+1-PN, where pi is the number I prime. Obviously there are d1=1 and the DN for n>1 is even. "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 (< 105), please 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 is one line, not exceeding N's number of prime pairs that satisfy the conjecture.

Input Sample:

20

Sample output:

4

The code is as follows:

1#include <stdio.h>2#include <math.h>3 inta[100005]={0};4 intIsPrime (intnum);5 intMain ()6 {7     intnum;8scanf"%d",&num);9     intflag1=2, flag2=0;Tena[2]=2; One      for(intI=3; i<=num;i++){ A         if(IsPrime (i)) { -flag1++; -A[FLAG1] =i; the             if((a[flag1]-a[flag1-1])==2){ -flag2++; -             } -         } +     } -printf"%d", Flag2); +     return 0; A } at intIsPrime (intnum) { -     intflag=1; -     inttemp=sqrt (num); -      for(intI=2; i<=temp;i++){ -         if(num%i==0){ -Flag =0; in         }  -     } to     returnFlag; +}

Thinking Analysis:

The Prime number table first writes 0, the second item is 2, and then iterates through is not the prime number. The prime pair satisfies the conjecture, and the counter is + +.

= = First of all, I'm going to have a better question for today.

pat_1007 prime number pair conjecture

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.