Skip to content

Instantly share code, notes, and snippets.

@pinglunliao
Last active November 5, 2019 08:40
Show Gist options
  • Save pinglunliao/fbf34421a8e082d9af7e to your computer and use it in GitHub Desktop.
Save pinglunliao/fbf34421a8e082d9af7e to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cmath>
int main(int argc, char** argv) {
bool isPrime;
int primeCnt;
unsigned long a, b;
//while(cin >> a >> b)
while(scanf("%d%d", &a, &b) != EOF)
{
//printf("%d %d\n", a, b);
primeCnt = 0;
for(unsigned long i = a; i <= b; i++)
{
isPrime = true;
if( i == 2 )
primeCnt++;
else if( i == 3 )
primeCnt++;
else if( i == 5 )
primeCnt++;
else if( i == 7 )
primeCnt++;
if( i == 1 || i % 2 == 0 || i % 3 == 0 || i % 5 == 0 || i % 7 == 0)
continue;
unsigned long sqrt_i = sqrt(i);
for(unsigned long k = 9; k <= sqrt_i; k += 2)
{
if(k % 2 == 0 || k % 3 == 0 || k % 5 == 0)
continue;
if( i % k == 0)
{
isPrime = false;
break;
}
}
if(isPrime == true)
{
//printf("Prime:%ld\n", i);
primeCnt++;
}
}
//cout << primeCnt << endl;
printf("%d\n", primeCnt);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment