-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeTester.java
More file actions
73 lines (71 loc) · 2.52 KB
/
PrimeTester.java
File metadata and controls
73 lines (71 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* This class will read in an int value and check
* if it is prime or not
* @author Michael Roscoe
*/
import java.util.InputMismatchException;
import java.util.Scanner;
public class PrimeTester {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Please enter an integer to check if it is prime: ");
try {
int prime = sc.nextInt();
System.out.println("Testing using loops:");
if (checkPrimeLoop(prime)) {
System.out.println("This number is a prime: " + prime);
} else {
System.out.println("This number is not a prime: " + prime);
}
System.out.println("Testing using recursion:");
if (checkPrimeRecursive(prime, prime, 0)) {
System.out.println("This number is a prime: " + prime);
} else {
System.out.println("This number is not a prime: " + prime);
}
}
catch (InputMismatchException e) {
System.out.println("Error: Non-integer value entered.");
}
}
/**
* This method checks whether a passed in int is a prime using iteration
* @param n The number to be checked for being a prime
* @return Indication of the number being prime or not
*/
public static boolean checkPrimeLoop(int n) {
boolean success;
if (n < 1) {
throw new IllegalArgumentException("Argument less than 1, illegal argument");
}
int count = 0;
for (int i = n; i > 0; i--) {
if (n%i == 0) {
count++;
}
}
success = (count == 2);
return success;
}
/**
* This method checks whether a passed in int is a prime using iteration
* @param n The number to be checked
* @param start The current value to be divided
* @param count The number of times that the division had a remainder of 0
* @return Indication of the number being prime or not
*/
public static boolean checkPrimeRecursive(int n, int start, int count) {
boolean success;
if (n < 1) {
throw new IllegalArgumentException("Argument less than 1, illegal argument");
}
if (start == 0) {
success = (count == 2);
} else if (n%start == 0) {
success = checkPrimeRecursive(n, start-1, count+1);
} else {
success = checkPrimeRecursive(n, start-1, count);
}
return success;
}
}