+ 2
What do y'all think about it? Another way to find prime numbers. How can the code be improved?
static public boolean IsPrime(int a){ if (a == 0){return false;} else if (a == 1){return false;} else if (a == 2)//The only even prime number is 2. {return true;} else if (a == 3){return true;} else if(a==5) {return true;} else if (a%2==0||a%3==0||a%5==0) {return false;} else {return true;} }
6 odpowiedzi
+ 5
Check these codes.
https://code.sololearn.com/c1oGS6Tssdp6/?ref=app
https://code.sololearn.com/cvTcXW7Q81a9/?ref=app
https://code.sololearn.com/cvdMmG9IFTlm/?ref=app
https://code.sololearn.com/cE1OxrgoWuBU/?ref=app
https://code.sololearn.com/cS8J8hTyFyRA/?ref=app
https://code.sololearn.com/cUG344RM9qwx/?ref=app
https://code.sololearn.com/c7o2U1DUoD4m/?ref=app
+ 5
David Bukedi You have used so many if else for this which not good and also 3-4 lines of code is going till 9-10 lines. And also that is not right code to check prime numbers.
+ 2
AJ #Level 20 End Day thank you but I just wanted to know your thoughts on this code 😅 does it make sense or not and why? Is there any possible improvement?
+ 2
AJ #Level 20 End Day thank you !
+ 2
Denise Roßberg thank you this will be very helpful ! And what could make it a wrong prime checker?
0
David Bukedi
You can work with boolean operators. And because of return statements you don't need else.
if(a == 0 || a == 1) rerurn false
if(a == 2 || a == 3 || a == 5) return true
if(a%2 == 0 || a % 3 == 0 || a % 5 == 0) return false
return true
But I am not sure if this would be a correct prime checker.