NYOJ26 Twin Prime problems

Source: Internet
Author: User

Offline calculation of all the structure within the prescribed range, not too difficult, mainly pay attention to efficiency, using sieve method to do.


The problem of twin primes

Time limit: ms | Memory limit:65535 KB

Difficulty:3

  • Describe

  • Write a program to find out the number of groups of all twin primes in the range of prime numbers. In general, the number of twins refers to the two prime number distance is 2, near the adjacent prime number can not be closer. Some children's shoes to see the problem began to write procedures, do not carefully read the question, we to curb the reading is not carefully carefully children's shoes, the two prime number adjacent to 1 is also a twin prime.

      • Input

      • The first line gives an N (0<n<100) indicating the number of test data groups.
        The next set of test data gives m, which means finding out all the twin primes before M.
        (0<m<1000000)

      • Output

      • Each set of test data output occupies one row, and the number of twins in the range of M ranges.

      • Sample input

      • 114
      • Sample output

      • 4


#include <iostream> #include <cstring> #include <cmath>using namespace Std;int a[1000000];void IsPrime (int number) {int i,j;for (i=1;i<=number;i++) if (i%2) a[i]=1;for (i=3;i<=sqrt (number); i+=2) if (A[i]) {for ( j=i*2;j<=number;j+=i) a[j]=0;}} int main () {int N,m,i,count;cin>>n;while (n--) {Cin>>m;memset (a,0,sizeof (a)); Count=0;isprime (m); for (i=2 ; i<=m-2;i++) if (A[i] && a[i+2])//Determine whether it is a prime number count++;if (m>=3) Count++;cout<<count<<endl;} return 0;}


NYOJ26 Twin Prime problems

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.