File tree Expand file tree Collapse file tree 2 files changed +7
-13
lines changed
src/com/codefortomorrow/advanced/chapter13 Expand file tree Collapse file tree 2 files changed +7
-13
lines changed Original file line number Diff line number Diff line change 11
11
as many numbers as they want until they enter -1.
12
12
*/
13
13
14
- public class PrimePractice {
15
-
16
- }
14
+ public class PrimePractice {}
Original file line number Diff line number Diff line change @@ -26,25 +26,21 @@ public static void main(String[] args) {
26
26
System .out .print ("Enter an integer to check: " );
27
27
s = reader .nextInt ();
28
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!" );
29
+ if (isPrime (s , 2 )) System .out .println (
30
+ "That is a prime!"
31
+ ); else System .out .println ("Not a prime!" );
33
32
}
34
33
}
35
34
}
36
35
37
36
//n is the number to check, z is the current number being divided
38
37
public static boolean isPrime (int n , int z ) {
39
38
//Check base cases
40
- if (n <= 2 )
41
- return (n == 2 ) ? true : false ;
39
+ if (n <= 2 ) return (n == 2 ) ? true : false ;
42
40
//Ternary operator used there
43
- if (n % z == 0 )
44
- return false ;
41
+ if (n % z == 0 ) return false ;
45
42
//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 ;
43
+ if (Math .pow (z , 2 ) > n ) return true ;
48
44
49
45
//If none of the above work
50
46
return isPrime (n , z + 1 );
You can’t perform that action at this time.
0 commit comments