-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrimeGenerator.java
More file actions
60 lines (52 loc) · 2.3 KB
/
Copy pathPrimeGenerator.java
File metadata and controls
60 lines (52 loc) · 2.3 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
package PrimeGenerator;
import java.util.ArrayList; // used to store the prime numbers
import java.util.Arrays; // used to initialize array
import java.util.Scanner;// used to get user input
public class PrimeGenerator {
public static void main(String[] args) {
if (args.length > 0) {
if (args.length == 1)
primeGenerator(Integer.parseInt(args[0]), 10000);
else if (args.length == 2)
primeGenerator(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
else
System.out.println("[!] Available arguments: [range] [max_int | optional]");
} else {
Scanner scan = new Scanner(System.in);
System.out.print("[->] Enter the number of terms: ");
int range = scan.nextInt();
scan.close();
primeGenerator(range, 10000);
}
} // end of main method
public static void primeGenerator(int number, int max_int) {
// this method is supposed to generate n prime numbers and print them.
int count = 0; // a counter variable to count the number of prime terms generated
boolean[] isPrime = new boolean[max_int + 1]; // to process whether a number is prime or not
ArrayList<Integer> primeList = new ArrayList<Integer>(number); // to store the prime numbers
Arrays.fill(isPrime, true); // setting all the values to true.
for (int i = 2; (int) Math.pow(i, 2) <= max_int; i++)
if (isPrime[i])
for (int j = (int) Math.pow(i, 2); j <= max_int; j += i)
isPrime[j] = false;
for (int i = 2;i <= max_int; i++){
if (isPrime[i]){
count++;
primeList.add(i); // to store the prime number on to a separate list.
if (count == number)
break;
}
}
System.out.println("Generated Prime Numbers ");
int col = 0;
for (int primeNumber: primeList) {
if (col % 10 == 0) {
System.out.println();
}
System.out.format("%10d", primeNumber);
col++;
}
// if you want to return the prime numbers, then use 'return primeList;' and also change the function
// definition as it is presently set to void.
}
}