1436 twin prime 2, 1436 prime

Source: Internet
Author: User

1436 twin prime 2, 1436 prime
1436 twin prime number 2

 

Time Limit: 2 s space limit: 1000 KB title level: Silver Title Description Description

For example, m = 100, n = 6

It will output all the twin prime numbers with 6 Differences of less than 100: for example,

5 11

7 13

....

83 89

Use a half-width space to distinguish between the output number and the number according to this rule, each row.

Input description Input Description

Enter an integer m in the first row as a range (for example, 100)

In the second row, enter an integer k as the tolerance of the target pair prime number (for example, 6)

Output description Output Description

Each row outputs a pair, and the last row outputs: Total Is :? (? Indicates the total number of such pairs,If not, the output Is Total Is: 0.)

Sample Input Sample Input

Example 1:

50 2

Example 2:

100 90

Example 3:

200 199

Sample output Sample Output

Example 1:

3 5
5 7
11 13
17 19
29 31
41 43
Total Is: 6

Example 2:

7 97
Total Is: 1

Example 3:

Total Is: 0

Data range and prompt Data Size & Hint

M <= 5000

CATEGORY tag Tags click here to expandThis question should have a more optimized algorithm, but I think m <= 5000 ...... let alone brute force!
 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 const int MAXN=10000001; 6 int vis[MAXN]; 7 int bc[MAXN]; 8 int now=1; 9 int main()10 {11     int m,n;12     scanf("%d%d",&m,&n);13     for(int i=2;i<=sqrt(m);i++)14     {15         if(vis[i]==0)16         {17             for(int j=i*i;j<=m;j=j+i)18             {19                 vis[j]=1;20             }21         }22     }23     /*int pre=-1;24     25     for(int i=2;i<=m;i++)26     {27         if(vis[i]==0)28         {29             if(i-pre==n)30             {31                 printf("%d %d\n",pre,i);32                 tot++;33             }34             pre=i;35         }36     }*/37     int tot=0;38     for(int i=2;i<=m;i++)39     {40         if(vis[i]==0)41         {42             bc[now]=i;43             now++;44         }45     }46     for(int i=1;i<=now-1;i++)47     {48         for(int j=1;j<=now-1;j++)49         {50             if(bc[j]-bc[i]==n)51             {52                 printf("%d %d\n",bc[i],bc[j]);53                 tot++;54             }55         }56     }57     printf("Total Is:%d",tot);58     return 0;59 }
 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 const int MAXN=10000001; 6 int vis[MAXN]; 7 int bc[MAXN]; 8 int now=1; 9 int main()10 {11     int m,n;12     scanf("%d%d",&m,&n);13     for(int i=2;i<=sqrt(m);i++)14     {15         if(vis[i]==0)16         {17             for(int j=i*i;j<=m;j=j+i)18             {19                 vis[j]=1;20             }21         }22     }23     /*int pre=-1;24     25     for(int i=2;i<=m;i++)26     {27         if(vis[i]==0)28         {29             if(i-pre==n)30             {31                 printf("%d %d\n",pre,i);32                 tot++;33             }34             pre=i;35         }36     }*/37     int tot=0;38     for(int i=2;i<=m;i++)39     {40         if(vis[i]==0)41         {42             bc[now]=i;43             now++;44         }45     }46     for(int i=1;i<=now-1;i++)47     {48         for(int j=1;j<=now-1;j++)49         {50             if(bc[j]-bc[i]==n)51             {52                 printf("%d %d\n",bc[i],bc[j]);53                 tot++;54             }55         }56     }57     printf("Total Is:%d",tot);58     return 0;59 }

 

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.