generate all prime number less than n java

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

showing results for - "generate all prime number less than n java"
Serena
28 May 2017
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}
queries leading to this page
find all prime numbers less than 100 gfg codeprime numbers upto ncount primes less than nreturn all prime numbers less than num 28int 29find all prime numbers less than nget all prime numbers less than n cppfind primes numbes from 1 to nprime numbers count until nfind all prime numbers less than n in a listprint all primes till ngiven a number n find the number of prime numbers less than equal to that number javacount number of prime numbers less than nprogram to print all prime numbers from 1 to 10 5e6find prime numbers till nprint all the prime numbers in array till the given limit fastest way to print primenumbers less than nfind all prime numbers between 1 to nprint prime number less than nnumber of prime nos within a given no in javacode to finad all prime numbers tillnfind all prime numbers smaller than nfind prime till n in java in less time complexityprint the prime numbers upto number nprint prime numbers upto less than n in creturn all primenumbers smaller than num 28int 29generate primes to nfind all prime numbers less than a prime numberget list of all prime numbers till nprime numbers till nprogram to print prime numbers less than the specified in javanumber of prime numbers till n javaminimum number of flips to make k length substring have at least one 1 in binary stringwhy prime numbers n 2an loophow to calculate all prime numbers less than ndisplay prime number less than 20 from a given list of numbershow to print prime numbers in java less than number nfind prime numbers upto nprint all prime numbersgiven a number n find the number of prime numbers less than equal to that number return all prime numbers less than num 28int 29 print prime numbers till ncalculate prime numbers from 1 to nnumber of primes less than n javajava function get all prime numbers less than 100print prime numbers less than nc 2b 2b code to print prime numbers till nfind all numbers below a given nfinding prime number less than 10 c 23how to program amount of primes less than nprogram to find all prime numbersfinding all prime numbers up to a specific integer5 make shell function which can find the last three prime number lesser than 100 count of primes less than n javagiven a number n find the number of prime numbers less than equal to that number generate all prime number less than n javaprime number less than in javarpinting primescode an algorithm to find all prime numbers less than n prime numbers less than n in pythonprint all the prime numbers till the given limit without function given an integer n 2c print all the prime numbers that lie in the range 2 to n 28both inclusive 29 print the prime numbers in different lines find all prime numbr less than n inc ppcount prime numbers less than n javacode an algorithm to find all prime numbers less than n pythonprinting prime numbers prime number bigger than 10000 using javafind prime numbers from 1 to ncount number of primes less than nprint prime numbersno of prime numbers till nall the prime numbers till n in javahow to find prime number which is less than targetprime number less than n in pythonfind prime numbers less than nyou are given a number n you have to tell how many prime numbers less than or equal to n exist for ex if n 3d10 2c your answer should be 4 as 7b2 2c 3 2c 5 2c 7 7d are primes below 10given integer n generate all prime numbers less than n java codejava take as input n determine all prime numbers till n and print them on the same line with spaces in betweenprint number of prime numbers less than nfind prime number less than nnumber of prime numbers upto nprimes till nprime numbers till n in javaprime less than nprint prime numbers from 2 to nprint all prime numbers less than or equal to nnumber of prime factors upto n in javareturn all prime numbers smaller than numtake as input n determine all prime numbers till n and print them on the same line with spaces in betweenmodify the code so it prints all primes less than or equal to n prime numbers less than k c 2b 2bprint all prime numbers till ngiven a positive integer n 2c find all primes less than n count prime numbers upto nminimum number of flips to make k substring at least one 1 in binary stringfind number of prime numbers less than nhow to get big prime number in javanumbers of length n and value less than k javano of prime numbers less than n c 2b 2bprint all the prime numbers till the given limitcompute all prime number less than a numberoptimal code to find number of prime numbers till nc program to find prime number less than ncode an algorithm to find all prime numbers less than n generate all prime number less than n java