+ 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;} }

12th Apr 2020, 2:43 PM
David Bukedi Diela
David Bukedi Diela - avatar
6 odpowiedzi
+ 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.
12th Apr 2020, 3:09 PM
A͢J
A͢J - avatar
+ 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?
12th Apr 2020, 3:06 PM
David Bukedi Diela
David Bukedi Diela - avatar
+ 2
12th Apr 2020, 3:19 PM
David Bukedi Diela
David Bukedi Diela - avatar
+ 2
Denise Roßberg thank you this will be very helpful ! And what could make it a wrong prime checker?
12th Apr 2020, 3:22 PM
David Bukedi Diela
David Bukedi Diela - avatar
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.
12th Apr 2020, 3:20 PM
Denise Roßberg
Denise Roßberg - avatar