ACM 3.2 Prime Number Screening method

Source: Internet
Author: User

Description

Xiao Ming's study of the logarithm of love, a talk about the number, the brain emerges a good majority of the problem, today, Xiao Ming wants to test your understanding of the prime number.
The problem is this: a decimal number, if it is prime, and its numbers and is also a prime number, it is called the "United States prime", such as 29, itself is a prime, and 2+9 = 11 is also prime, so it is the United States prime.
Given an interval, can you figure out how many primes there are in this interval?

Input

The first line enters a positive integer t, which indicates that there is a total of T-group data (T <= 10000).
Next, a total of two integers l,r (1<= L <= R <= 1000000) are entered for each line, representing the left and right values of the interval.

Output

For each set of data, the number of case is first output, and then the number of us primes in the interval (including the endpoint value l,r) is output.
One row for each group of data, and a specific output format see sample.

Sample Input

31 1002 23 19

Sample Output

Case #1:14Case #2:1Case #3:4 Problem Solving Ideas: First we make clear that this is a problem of the number of filters, we will judge the prime number, the table. We use the table to solve this problem, we first step from the 0~1000000 to filter out the prime number, the second from these prime numbers to filter out the number of us, and finally we hit the table. So when we enter the range, we can get a direct estimate of the number of us in this range. Program code:
#include <iostream> #include <cmath>using namespace std, #define PR 1000005int u[pr]={0};int dp[pr];void Prime () {int i,j;u[0]=1,u[1]=1;//is not a prime for (i=2;i<pr;i++) {if (!u[i]) {for (j=2;i*j<pr;j++)    u[i*j]=1;//is not a prime number}} for (i=2;i<pr;i++) {if (u[i]!=1)//is the prime number {int m=i,sum=0;while (M>0)//continuously gets the digits on each bit {sum+=m%10;m/=10;} if (u[sum]!=1)//To determine the sum of the numbers on each bit is also a prime   here can only be written u[sum]!=1 can not be written u[sum]==0 or!u[sum]              //Because it may be equal to 2, because it may have been marked as 2 u[i ]=2;//i is a U.S. prime number, which is marked as 2}}, int main () {prime (), int sum1=0;for (int j=0;j<pr;j++) {if (u[j]==2)//is a US prime number sum1++;DP [j]=sum1 ;//Play Table}int T,k=0;cin>>t;while (t--) {int m,n;cin>>m>>n;int re=0;re=dp[n]-dp[m];if (u[m]==2)// To determine if M is also within this range, add 1  re++;cout<< "Case #" <<++k<< ":" <<re<<endl;} return 0;}

  

ACM 3.2 Prime Number Screening method

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.