Last active
November 5, 2019 08:40
-
-
Save pinglunliao/fbf34421a8e082d9af7e to your computer and use it in GitHub Desktop.
a121: 質數又來囉 FYI: https://yunlinsong.blogspot.com/2015/02/blog-post.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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