generate all prime number less than n java

/**
Author: Jeffrey Huang
As far as I know this is almost the fastest method in java
for generating prime numbers less than n.
A way to make it faster would be to implement Math.sqrt(i)
instead of i/2.

I don't know if you could implement sieve of eratosthenes in 
this, but if you could, then it would be even faster.

If you have any improvements please email me at
[email protected].
 */


import java.util.*;
 
public class Primecounter {
    
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
      //int i =0;
      int num =0;
      //Empty String
      String  primeNumbers = "";
      boolean isPrime = true;
      System.out.print("Enter the value of n: ");
      System.out.println();
      int n = scanner.nextInt();

      for (int i = 2; i < n; i++) {
         isPrime = true;
         for (int j = 2; j <= i/2; j++) {
            if (i%j == 0) {
               isPrime = false; 
            }
         }
         if (isPrime)
         System.out.print(" " + i);
      }	

    }
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
count of primes less than n java number of primes less than n java how to print prime numbers in java less than number n prime number bigger than 10000 using java Given a number n find the number of prime numbers less than equal to that number. java get all prime numbers less than n cpp program to print prime numbers less than the specified in java code to finad all prime numbers tilln number of prime numbers till n java print the prime numbers upto number n count number of prime numbers less than n print number of prime numbers less than n rpinting primes count prime numbers upto n prime numbers upto n number of prime numbers upto n print prime numbers less than n all the prime numbers till n in java count number of primes less than n Given a number n find the number of prime numbers less than equal to that number Given a number n find the number of prime numbers less than equal to that number. find primes numbes from 1 to n number of prime nos within a given no in java find prime numbers less than n find all prime numbers smaller than n fastest way to print primenumbers less than n c program to find prime number less than n print prime number less than n find all prime numbr less than n inc pp find all prime numbers between 1 to n count primes less than n get list of all prime numbers till n compute all prime number less than a number Given a positive integer n, find all primes less than n. how to calculate all prime numbers less than n prime number less than n in python Print all the prime numbers till the given limit without function print all prime numbers less than or equal to n 5. Make shell function which can find the last three prime number lesser than 100. printing prime numbers. find prime number less than n prime numbers count until n primes till N find all prime numbers less than n in a list no. of prime numbers less than n c++ prime numbers less than k c++ c++ code to print prime numbers till n Print all the prime numbers in array till the given limit Print all the prime numbers till the given limit how to find prime number which is less than target calculate prime numbers from 1 to n find prime till n in java in less time complexity You are given a number n. You have to tell how many prime numbers less than or equal to n exist. For ex. if n=10, your answer should be 4 as {2, 3, 5, 7} are primes below 10 Return all prime numbers less than num (int) Return all prime numbers less than num (int). return all prime numbers smaller than num Return all primenumbers smaller than num (int) prime less than n prime numbers till n print all prime numbers till n find prime numbers upto n print prime numbers upto less than n in c prime numbers till n in java Print Prime Numbers from 2 to N find all prime numbers less than 100 gfg code Modify the code so it prints all primes less than or equal to n. print all prime numbers program to print all prime numbers from 1 to 10^6 generate primes to n prime numbers less than n in python Code an algorithm to find all prime numbers less than n Code an algorithm to find all prime numbers less than n python Code an algorithm to find all prime numbers less than n. print prime numbers till n java Take as input n. Determine all prime numbers till n and print them on the same line with spaces in between Take as input n. Determine all prime numbers till n and print them on the same line with spaces in between find all prime numbers less than a prime number find all prime numbers less than n program to find ALL prime numbers find prime numbers till n find number of prime numbers less than n Display prime number less than 20 from a given list of numbers print prime numbers print all primes till n Given an integer N, print all the prime numbers that lie in the range 2 to N (both inclusive). Print the prime numbers in different lines. find prime numbers from 1 to n prime number less than in java optimal code to find number of prime numbers till n why prime numbers n*n loop number of prime factors upto n in java no of prime numbers till n how to program amount of primes less than n find all numbers below a given n finding all prime numbers up to a specific integer finding prime number less than 10 c# numbers of length n and value less than k java minimum number of flips to make k length substring have at least one 1 in binary string minimum number of flips to make k substring at least one 1 in binary string generate all prime number less than n java
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source