JAVA
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
// This programm will return the first n prime numbers. It is probably the most efficient program to do this.
// edit a year later: is it really?
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
int n = Scan.nextInt();
int[] prime = new int[n];
prime[0] = 2;
int k=3;
for (int i=1; i<n; i++) {
for (int j=0; j<i; j++) {
if (k % prime[j] == 0) {
k +=2;
j=0;
}
}
prime[i] = k;
k +=2;
}
for(int a: prime) {
System.out.print(a+", ");
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run