Note: AJMansfield (in a comment and in this answer (+1 - nice answer) in this answer (+1 - nice answer)) has pointed out that the code is buggy. It is, and this answer missed that. The algorithm in your code (and my reproduction here), is wrong.
As AJ says, your for-loop should start at 3, not 5.
If you have to test just one number, and never more than one number, then the solution you propose is not too bad. There are some tweaks available, and they will improve performance, but the algorithm itself is decent.
The code style is inconsistent and non-standard though.
1-liner if-statements are a known code-style contravention, yet you do it in a few places:
if (num <= 3){ return num > 1; } if ((num & 1) == 0) { return false; }
You may want to argue that you can do this in your own style, but, you are inconsistent with it, and that's even worse than the 1-liner. If you are going to use 1-liners for simple conditions, then do it always (and the following should be a 1-liner too):
if (num % i == 0) { return false; }
As for your current implementation's performance, there's only one significant issue I see:
for (int i = 5; i * i <= num; i += 2) {
The above code calculates the \$i^2\$ on each iteration. It would be better to calculate the square-root of num
first, and then limit the loop that way:
int limit = (int)Math.sqrt(num);
for (int i = 5; i <= limit; i += 2) {
Note: AJMansfield (in a comment and in this answer (+1 - nice answer)) has pointed out that the code is buggy. It is, and this answer missed that. The algorithm in your code (and my reproduction here), is wrong.
As AJ says, your for-loop should start at 3, not 5.
If you have to test just one number, and never more than one number, then the solution you propose is not too bad. There are some tweaks available, and they will improve performance, but the algorithm itself is decent.
The code style is inconsistent and non-standard though.
1-liner if-statements are a known code-style contravention, yet you do it in a few places:
if (num <= 3){ return num > 1; } if ((num & 1) == 0) { return false; }
You may want to argue that you can do this in your own style, but, you are inconsistent with it, and that's even worse than the 1-liner. If you are going to use 1-liners for simple conditions, then do it always (and the following should be a 1-liner too):
if (num % i == 0) { return false; }
As for your current implementation's performance, there's only one significant issue I see:
for (int i = 5; i * i <= num; i += 2) {
The above code calculates the \$i^2\$ on each iteration. It would be better to calculate the square-root of num
first, and then limit the loop that way:
int limit = (int)Math.sqrt(num);
for (int i = 5; i <= limit; i += 2) {
Note: AJMansfield (in a comment and in this answer (+1 - nice answer)) has pointed out that the code is buggy. It is, and this answer missed that. The algorithm in your code (and my reproduction here), is wrong.
As AJ says, your for-loop should start at 3, not 5.
If you have to test just one number, and never more than one number, then the solution you propose is not too bad. There are some tweaks available, and they will improve performance, but the algorithm itself is decent.
The code style is inconsistent and non-standard though.
1-liner if-statements are a known code-style contravention, yet you do it in a few places:
if (num <= 3){ return num > 1; } if ((num & 1) == 0) { return false; }
You may want to argue that you can do this in your own style, but, you are inconsistent with it, and that's even worse than the 1-liner. If you are going to use 1-liners for simple conditions, then do it always (and the following should be a 1-liner too):
if (num % i == 0) { return false; }
As for your current implementation's performance, there's only one significant issue I see:
for (int i = 5; i * i <= num; i += 2) {
The above code calculates the \$i^2\$ on each iteration. It would be better to calculate the square-root of num
first, and then limit the loop that way:
int limit = (int)Math.sqrt(num);
for (int i = 5; i <= limit; i += 2) {
Note: AJMansfield (in a comment and in this answer (+1 - nice answer) ) has pointed out that the code is buggy. It is, and this answer missed that. The algorithm in your code (and my reproduction here), is wrong.
As AJ says, your for-loop should start at 3, not 5.
If you have to test just one number, and never more than one number, then the solution you propose is not too bad. There are some tweaks available, and they will improve performance, but the algorithm itself is decent.
The code style is inconsistent and non-standard though.
1-liner if-statements are a known code-style contravention, yet you do it in a few places:
if (num <= 3){ return num > 1; } if ((num & 1) == 0) { return false; }
You may want to argue that you can do this in your own style, but, you are inconsistent with it, and that's even worse than the 1-liner. If you are going to use 1-liners for simple conditions, then do it always (and the following should be a 1-liner too):
if (num % i == 0) { return false; }
As for your current implementation's performance, there's only one significant issue I see:
for (int i = 5; i * i <= num; i += 2) {
The above code calculates the \$i^2\$ on each iteration. It would be better to calculate the square-root of num
first, and then limit the loop that way:
int limit = (int)Math.sqrt(num);
for (int i = 5; i <= limit; i += 2) {
If you have to test just one number, and never more than one number, then the solution you propose is not too bad. There are some tweaks available, and they will improve performance, but the algorithm itself is decent.
The code style is inconsistent and non-standard though.
1-liner if-statements are a known code-style contravention, yet you do it in a few places:
if (num <= 3){ return num > 1; } if ((num & 1) == 0) { return false; }
You may want to argue that you can do this in your own style, but, you are inconsistent with it, and that's even worse than the 1-liner. If you are going to use 1-liners for simple conditions, then do it always (and the following should be a 1-liner too):
if (num % i == 0) { return false; }
As for your current implementation's performance, there's only one significant issue I see:
for (int i = 5; i * i <= num; i += 2) {
The above code calculates the \$i^2\$ on each iteration. It would be better to calculate the square-root of num
first, and then limit the loop that way:
int limit = (int)Math.sqrt(num);
for (int i = 5; i <= limit; i += 2) {
Note: AJMansfield (in a comment and in this answer (+1 - nice answer) ) has pointed out that the code is buggy. It is, and this answer missed that. The algorithm in your code (and my reproduction here), is wrong.
As AJ says, your for-loop should start at 3, not 5.
If you have to test just one number, and never more than one number, then the solution you propose is not too bad. There are some tweaks available, and they will improve performance, but the algorithm itself is decent.
The code style is inconsistent and non-standard though.
1-liner if-statements are a known code-style contravention, yet you do it in a few places:
if (num <= 3){ return num > 1; } if ((num & 1) == 0) { return false; }
You may want to argue that you can do this in your own style, but, you are inconsistent with it, and that's even worse than the 1-liner. If you are going to use 1-liners for simple conditions, then do it always (and the following should be a 1-liner too):
if (num % i == 0) { return false; }
As for your current implementation's performance, there's only one significant issue I see:
for (int i = 5; i * i <= num; i += 2) {
The above code calculates the \$i^2\$ on each iteration. It would be better to calculate the square-root of num
first, and then limit the loop that way:
int limit = (int)Math.sqrt(num);
for (int i = 5; i <= limit; i += 2) {
If you have to test just one number, and never more than one number, then the solution you propose is not too bad. There are some tweaks available, and they will improve performance, but the algorithm itself is decent.
The code style is inconsistent and non-standard though.
1-liner if-statements are a known code-style contravention, yet you do it in a few places:
if (num <= 3){ return num > 1; } if ((num & 1) == 0) { return false; }
You may want to argue that you can do this in your own style, but, you are inconsistent with it, and that's even worse than the 1-liner. If you are going to use 1-liners for simple conditions, then do it always (and the following should be a 1-liner too):
if (num % i == 0) { return false; }
As for your current implementation's performance, there's only one significant issue I see:
for (int i = 5; i * i <= num; i += 2) {
The above code calculates the \$i^2\$ on each iteration. It would be better to calculate the square-root of num
first, and then limit the loop that way:
int limit = (int)Math.sqrt(num);
for (int i = 5; i <= limit; i += 2) {