Skip to content

Commit 5b614d8

Browse files
committed
Clarify code, add braces, remove Math import
1 parent 21dc0a3 commit 5b614d8

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
Write a method called isPrime which returns
55
true if the given integer is prime and false otherwise.
66
7-
This is similar to the chapter 11 problem, but this time write
7+
This is similar to the Chapter 11 problem, but this time write
88
your method using recursion.
99
10-
In your main method, include a scanner so the user can check
10+
In your main method, include a Scanner so the user can check
1111
as many numbers as they want until they enter -1.
12+
13+
Note: There are more complex solutions, but this is the fastest one
14+
within the scope of this chapter.
1215
*/
1316

14-
public class PrimePractice {}
17+
public class PrimePractice {
18+
// write code here
19+
}
Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,68 @@
1-
package com.codefortomorrow.advanced.chapter13.practice;
1+
package com.codefortomorrow.advanced.chapter13.solutions;
2+
3+
import java.util.Scanner;
24

35
/*
46
Write a method called isPrime which returns
57
true if the given integer is prime and false otherwise.
68
7-
This is similar to the chapter 11 problem, but this time write
9+
This is similar to the Chapter 11 problem, but this time write
810
your method using recursion.
911
10-
In your main method, include a scanner so the user can check
12+
In your main method, include a Scanner so the user can check
1113
as many numbers as they want until they enter -1.
1214
1315
Note: There are more complex solutions, but this is the fastest one
1416
within the scope of this chapter.
1517
*/
1618

17-
import java.lang.Math;
18-
import java.util.Scanner;
19-
2019
public class PrimePractice {
2120

2221
public static void main(String[] args) {
23-
int s = 0;
24-
while (s != -1) {
25-
Scanner reader = new Scanner(System.in);
22+
Scanner reader = new Scanner(System.in);
23+
int number = 0;
24+
while (number != -1) {
2625
System.out.print("Enter an integer to check: ");
27-
s = reader.nextInt();
28-
if (s != -1) {
29-
if (isPrime(s, 2)) System.out.println(
30-
"That is a prime!"
31-
); else System.out.println("Not a prime!");
26+
number = reader.nextInt();
27+
if (number != -1) {
28+
if (isPrime(number, 2)) {
29+
System.out.println("That is a prime!");
30+
} else {
31+
System.out.println("Not a prime!");
32+
}
3233
}
3334
}
35+
reader.close();
3436
}
3537

3638
/**
3739
* isPrime checks the primality of a given integer
38-
* @param n The integer to check
39-
* @param z Current Divisor(used for recursion)
40+
* @param n The integer to check
41+
* @param z Current divisor (used for recursion)
4042
* @return boolean true if prime and false if not
4143
*/
4244
public static boolean isPrime(int n, int z) {
43-
//Check base cases
44-
if (n <= 2) return (n == 2) ? true : false;
45-
//Ternary operator used there
46-
if (n % z == 0) return false;
47-
//If z gets high enough that z > sqrt(n), then n is prime, because factors just repeat after
48-
if (Math.pow(z, 2) > n) return true;
49-
50-
//If none of the above work
45+
// Check base cases
46+
if (n <= 2) {
47+
return n == 2;
48+
}
49+
50+
// If n is divisible by the current divisor,
51+
// it has a factor other than 1 and thus is
52+
// not prime
53+
if (n % z == 0) {
54+
return false;
55+
}
56+
57+
// If z gets high enough that z > sqrt(n), then n is prime,
58+
// because factors just repeat after
59+
if (z > Math.sqrt(n)) {
60+
return true;
61+
}
62+
63+
// If none of the above work,
64+
// keep calling isPrime recursively
65+
// with a larger divisor
5166
return isPrime(n, z + 1);
5267
}
5368
}

0 commit comments

Comments
 (0)