HDU1431: prime number input

Source: Internet
Author: User

Problem Description
Xiaoou33 is particularly interested in both prime numbers and the number of input objects. For example, 151 is both a prime number and a background. Xiaoou333 wants you to help him find the prime number in a certain range. Please write a program to find the number of prime numbers that meet the conditions between a and B. (5 <= a <B <= 100,000,000 );

 


Input
There are many groups of data, each of which includes two groups of data: a and B.
 


Output
For each group of data, the return number of all prime numbers (including a and B) that meet the conditions between a and B is output, and a row is left blank after each group of data.
 


Sample Input
5 500


Sample Output
5
7
11
101
131
151
181
191
313
353
373
383

 

 

To be honest, you have to be amazed at others' ideas.

Because the int type occupies four bytes

To avoid space occupation, define the array as the bool type for processing.

 

 

[Cpp]
# Include <stdio. h>
# Include <stdio. h>
Using namespace std;
 
Boolean a [9989900];
Int prime [1005];
 
Void set ()
{
Int I, j;
For (I = 2; I <= 9989899; I + = 2)
A [I] = true;
For (I = 3; I <= 3161; I ++)
{
If (a [I])
Continue;
For (j = I + I; j <= 9989899; j + = I)
A [j] = true;
}
}
 
Int huiwen (int n)
{
Int a = 0, B = n;
While (B)
{
Int r = B % 10;
A = a * 10 + r;
B/= 10;
}
If (a = n)
Return 1;
Return 0;
}
 
Int main ()
{
Int n, m, I, k = 2;
Set ();
Prime [0] = 5;
Prime [1] = 7;
For (I = 11; I <= 9989899; I + = 2)
{
If (! A [I] & huiwen (I ))
Prime [k ++] = I;
}
While (~ Scanf ("% d", & n, & m ))
{
For (I = 0; I <k; I ++)
{
If (prime [I] <n)
Continue;
Else if (prime [I]> m)
Break;
Else
Printf ("% d \ n", prime [I]);
}
Printf ("\ n ");
}
Return 0;
}

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.