Skip to content

Commit 615302e

Browse files
committed
Fix Formatting II
1 parent 0d1c797 commit 615302e

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

src/com/codefortomorrow/advanced/chapter13/solutions/PrimePractice.java

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,34 @@
1818
import java.util.Scanner;
1919

2020
public class PrimePractice {
21-
22-
public static void main(String[] args) {
23-
int s = 0;
24-
while (s != -1) {
25-
Scanner reader = new Scanner(System.in);
26-
System.out.print("Enter an integer to check: ");
27-
s = reader.nextInt();
28-
if (s != -1) {
29-
if (isPrime(s, 2))
30-
System.out.println("That is a prime!");
31-
else
32-
System.out.println("Not a prime!");
33-
}
34-
}
35-
}
36-
37-
//n is the number to check, z is the current number being divided
38-
public static boolean isPrime(int n, int z) {
39-
//Check base cases
40-
if (n <= 2)
41-
return (n == 2) ? true : false;
42-
//Ternary operator used there
43-
if (n % z == 0)
44-
return false;
45-
//If z gets high enough that z > sqrt(n), then n is prime, because factors just repeat after
46-
if (Math.pow(z, 2) > n)
47-
return true;
48-
49-
//If none of the above work
50-
return isPrime(n, z + 1);
21+
public static void main(String[] args) {
22+
int s = 0;
23+
while (s != -1) {
24+
Scanner reader = new Scanner(System.in);
25+
System.out.print("Enter an integer to check: ");
26+
s = reader.nextInt();
27+
if (s != -1) {
28+
if (isPrime(s, 2))
29+
System.out.println("That is a prime!");
30+
else
31+
System.out.println("Not a prime!");
32+
}
5133
}
34+
}
35+
36+
//n is the number to check, z is the current number being divided
37+
public static boolean isPrime(int n, int z) {
38+
//Check base cases
39+
if (n <= 2)
40+
return (n == 2) ? true : false;
41+
//Ternary operator used there
42+
if (n % z == 0)
43+
return false;
44+
//If z gets high enough that z > sqrt(n), then n is prime, because factors just repeat after
45+
if (Math.pow(z, 2) > n)
46+
return true;
47+
48+
//If none of the above work
49+
return isPrime(n, z + 1);
50+
}
5251
}

0 commit comments

Comments
 (0)