-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimes_within_range.java
167 lines (159 loc) · 4.89 KB
/
primes_within_range.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.ArrayList;
import java.util.Scanner;
public class primes_within_range {public static void main(String[] args) {
primes();
}
public static void primes() {
//open input scanner
Scanner in = new Scanner(System.in);
//Defensive program for selection between range and amount
System.out.println("Do you need the primes within a range, or an amount from a starting position ( '1' for range or '2' for amount?");
boolean isGoodInput = false;
int choice = 0;
while(!isGoodInput){
try {
choice = in.nextInt();
if (choice == 1 || choice == 2){
isGoodInput=true;
} else {
System.out.println("Invalid input for choice. Please try again:");
continue;
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input for choice. Please try again:");
in.next();
continue;
}
}
if (choice == 1){
//Defensive program for selection of range
System.out.println("Where should the primes start from (pick a natural number larger than 1)?");
long min = 2;
long max = 2;
try {
min = in.nextLong();
if (min<2) {
System.out.println("Invalid input for value from minimum prime.");
System.out.println("Minimum prime set to 2.");
min = 2;
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input for value from minimum prime.");
System.out.println("Minimum prime set to 2.");
min = 2;
}
System.out.println("Where should the primes go up to (pick a natural number larger than your minimum)?");
//ensure max>min
try {
max = in.nextLong();
if (max<min) {
System.out.println("Invalid input for value from maximum prime.");
System.out.println("Minimum prime set to minimum.");
max = min;
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input for value from maximum prime.");
System.out.println("Minimum prime set to minimum.");
max = min;
}
//Close scanner
in.close();
//Creation of prime list
long numPrimesFound = 0;
ArrayList<Long> primes = new ArrayList<Long>();
boolean isPrime;
//check factors up to square root of checked number
for (long index = min; index<=max; index++) {
isPrime = true;
long maxFactorOfIndex = (long)Math.sqrt(index);
long factorToCheck = maxFactorOfIndex;
while (factorToCheck>1) {
if (index%factorToCheck == 0) {
isPrime = false;
break;
} else {
factorToCheck--;
}
}
//add to list of primes if while loop exited naturally
if (isPrime) {
numPrimesFound++;
primes.add(index);
}
}
//print results
if (numPrimesFound==0) {
System.out.printf("No primes found in the range from %d to %d.", min, max);
} else {
System.out.printf("The following primes in the range from %d to %d are:\n", min, max);
for (int idx = 0; idx < primes.size(); idx++) {
System.out.println(primes.get(idx) + ", ");
}
}
} else if (choice == 2) {
//Defensive program for selection of range
System.out.println("Where should the primes start from (pick a natural number larger than 1)?");
long start = 2;
long amount = 1;
//need a valid start point
try {
start = in.nextLong();
if (start<2) {
System.out.println("Invalid input for starting value.");
System.out.println("Start point set to 2.");
start = 2;
}
//catching input exceptions
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input for starting value.");
System.out.println("Start point set to 2.");
start = 2;
}
System.out.println("How many primes do you want (pick a natural number)?");
//ensure valid amount
try {
amount = in.nextLong();
if (amount<1) {
System.out.println("Invalid input for value of amount.");
System.out.println("Amount set to 1.");
amount=1;
}
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input for value of amount.");
System.out.println("Amount set to 1.");
amount=1;
}
//Close scanner
in.close();
long numPrimesFound = 0;
ArrayList<Long> primes = new ArrayList<Long>();
boolean isPrime;
//check factors up to square root of checked number
long index = start;
while (numPrimesFound < amount){
isPrime = true;
long maxFactorOfIndex = (long)Math.sqrt(index);
long factorToCheck = maxFactorOfIndex;
while (factorToCheck>1) {
if (index%factorToCheck == 0) {
isPrime = false;
break;
} else {
factorToCheck--;
}
}
//add to list of primes if while loop exited naturally
if (isPrime) {
numPrimesFound++;
primes.add(index);
}
index++;
}
//print results
System.out.printf("The first %d primes starting from %d are:\n", amount, start);
for (int idx = 0; idx < primes.size(); idx++) {
System.out.println(primes.get(idx) + ", ");
}
}
}
}