+ 1

Anyone can code a program to find prime nums in a range?

7th Jan 2017, 9:38 AM
School Dog
2 odpowiedzi
+ 3
I post the class that determines if a number is prime: bool isPrime(int x) { if (x<=0) return false; if (x<2) return true; for (int i=2;i<x/2+1;i++) { if (x%i==0) return false; } return true; } A nice optimization would be starting the for loop from 3 and incrementing i by 2.. Otherwise look on the web the best optimization to use in coding languages..
7th Jan 2017, 9:53 AM
Michael Isac Girardi
Michael Isac Girardi - avatar
+ 2
Please look at my c++ code, you would find a class to determine if a number is prime. It is not the best optimization because there are many way to look if a number is prime but I used the most understanding.. https://code.sololearn.com/c8gjJwr0o455/?ref=app
7th Jan 2017, 9:49 AM
Michael Isac Girardi
Michael Isac Girardi - avatar