1/**
2Author: Jeffrey Huang
3As far as I know this is almost the fastest method in java
4for generating prime numbers less than n.
5A way to make it faster would be to implement Math.sqrt(i)
6instead of i/2.
7
8I don't know if you could implement sieve of eratosthenes in
9this, but if you could, then it would be even faster.
10
11If you have any improvements please email me at
12jeffreyhuang9999@gmail.com.
13 */
14
15
16import java.util.*;
17
18public class Primecounter {
19
20 public static void main(String[] args) {
21 Scanner scanner = new Scanner(System.in);
22 //int i =0;
23 int num =0;
24 //Empty String
25 String primeNumbers = "";
26 boolean isPrime = true;
27 System.out.print("Enter the value of n: ");
28 System.out.println();
29 int n = scanner.nextInt();
30
31 for (int i = 2; i < n; i++) {
32 isPrime = true;
33 for (int j = 2; j <= i/2; j++) {
34 if (i%j == 0) {
35 isPrime = false;
36 }
37 }
38 if (isPrime)
39 System.out.print(" " + i);
40 }
41
42 }
43}