0

Write a c ++ code to evaluate prime numbers Fromm 500mil to 2 bill and should run in less than 30 seconds.

20th May 2019, 2:14 PM
Mike Rimoy
Mike Rimoy - avatar
4 Réponses
+ 3
These numbers might be too big for integers. Also, you don't need to check every number since a number > 2 that is divisible by 2 won't be a prime number. So you can start with an odd number and go +=2. The prime number algorithm isn't very efficient, especially not for big numbers, and the variable "count" doesn't seem to be declared anywhere
20th May 2019, 2:37 PM
Anna
Anna - avatar
+ 6
please show your try, and don't post assignments here
20th May 2019, 2:20 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
for(int x=500000000;x<=1000000000;x++){ if(x%2!=0) { for(int p=3;p<x/2;p+=2){ if(x%p==0) count++;} if(count==1) cout<<x<<'\n';} }
20th May 2019, 2:27 PM
Mike Rimoy
Mike Rimoy - avatar
0
Thank you Anna
20th May 2019, 6:00 PM
Mike Rimoy
Mike Rimoy - avatar