generate all prime number less than n java 28fastest method 29

Solutions on MaxInterview for generate all prime number less than n java 28fastest method 29 by the best coders in the world

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