|
1 |
| -package com.codefortomorrow.advanced.chapter13.practice; |
| 1 | +package com.codefortomorrow.advanced.chapter13.solutions; |
| 2 | + |
| 3 | +import java.util.Scanner; |
2 | 4 |
|
3 | 5 | /*
|
4 | 6 | Write a method called isPrime which returns
|
5 | 7 | true if the given integer is prime and false otherwise.
|
6 | 8 |
|
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 |
8 | 10 | your method using recursion.
|
9 | 11 |
|
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 |
11 | 13 | as many numbers as they want until they enter -1.
|
12 | 14 |
|
13 | 15 | Note: There are more complex solutions, but this is the fastest one
|
14 | 16 | within the scope of this chapter.
|
15 | 17 | */
|
16 | 18 |
|
17 |
| -import java.lang.Math; |
18 |
| -import java.util.Scanner; |
19 |
| - |
20 | 19 | public class PrimePractice {
|
21 | 20 |
|
22 | 21 | 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) { |
26 | 25 | 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 | + } |
32 | 33 | }
|
33 | 34 | }
|
| 35 | + reader.close(); |
34 | 36 | }
|
35 | 37 |
|
36 | 38 | /**
|
37 | 39 | * 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) |
40 | 42 | * @return boolean true if prime and false if not
|
41 | 43 | */
|
42 | 44 | 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 |
51 | 66 | return isPrime(n, z + 1);
|
52 | 67 | }
|
53 | 68 | }
|
0 commit comments