generate all prime number less than n java
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);
}
}
}
Also in Java:
- Title
- java generic calling clone method
- Category
- Java
- Title
- how to count the number of occurrences of an element in a arraylist in java
- Category
- Java
- Title
- how to open a text file in java
- Category
- Java
- Title
- jtable get get row
- Category
- Java
- Title
- java remove duplicates
- Category
- Java
- Title
- java lerp
- Category
- Java
- Title
- Java create array of array
- Category
- Java
- Title
- compare list from db and list from request
- Category
- Java
- Title
- java animated gif example
- Category
- Java
- Title
- print a string java
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- how to know if String is the same java
- Category
- Java
- Title
- how to check the end of a string java
- Category
- Java
- Title
- java console write
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- java check if string is number
- Category
- Java
- Title
- convert date to calendar java
- Category
- Java
- Title
- run static method java
- Category
- Java
- Title
- java how to convert string to int
- Category
- Java
- Title
- bukkit java get player count
- Category
- Java
- Title
- in javascipt how to stop further page processing
- Category
- Java
- Title
- java replace element in list
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- java how to compare strings
- Category
- Java
- Title
- iterate through hashmap in java
- Category
- Java
- Title
- java string format .2f
- Category
- Java
- Title
- how to scan a character in java
- Category
- Java
- Title
- inline foreach java
- Category
- Java
- Title
- Java system load from resources
- Category
- Java
- Title
- python to java converter
- Category
- Java
- Title
- esponente in java
- Category
- Java
- Title
- java - get open ports
- Category
- Java
- Title
- java newinstance alternative
- Category
- Java
- Title
- how to bubblesort a string array in java
- Category
- Java
- Title
- how to change double to int in java
- Category
- Java
- Title
- spannable string in android java for color
- Category
- Java
- Title
- onbackpressed android
- Category
- Java
- Title
- prime factorization java
- Category
- Java
- Title
- NumSelfDivisors java
- Category
- Java
- Title
- how to know when user is done typing android
- Category
- Java
- Title
- byte java
- Category
- Java
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- how do i print text in java
- Category
- Java
- Title
- para que sirve getcontentpane en java
- Category
- Java
- Title
- android how to know when snackbar is done
- Category
- Java
- Title
- get day name from date in java
- Category
- Java
- Title
- how to do for each in java
- Category
- Java
- Title
- hide element selenium
- Category
- Java
- Title
- java read file text
- Category
- Java
- Title
- java 8 stream option
- Category
- Java
- Title
- java check if a line is enclosed in quotation marks
- Category
- Java
- Title
- nikita kaksharov
- Category
- Java
- Title
- android bottom navigation hiding views
- Category
- Java
- Title
- runtime java examples
- Category
- Java
- Title
- java getter
- Category
- Java
- Title
- how to make one java class inherit from another
- Category
- Java
- Title
- absolute value in java
- Category
- Java
- Title
- how take inputs in java with message
- Category
- Java
- Title
- processing sound android
- Category
- Java
- Title
- get host from request object java
- Category
- Java
- Title
- if en une seul ligne java
- Category
- Java
- Title
- reading string after double in java
- Category
- Java
- Title
- java make arraylist
- Category
- Java
- Title
- how to get witdth of window android
- Category
- Java
- Title
- in java how to convert string to integer
- Category
- Java
- Title
- how to change the icon of a jframe
- Category
- Java
- Title
- java how to print a newline
- Category
- Java
- Title
- on click android studio not working
- Category
- Java
- Title
- spigot spawn entity
- Category
- Java
- Title
- expression régulière seulement un espace java
- Category
- Java
- Title
- how to change the color of a menubar in java
- Category
- Java
- Title
- android cardview dependency
- Category
- Java
- Title
- java string to lower case
- Category
- Java
- Title
- change the value in a hashtable java
- Category
- Java
- Title
- java script dynamic varibale creation
- Category
- Java
- Title
- usaco 2018 january contest
- Category
- Java
- Title
- refrence xml textfield in javafx
- Category
- Java
- Title
- what is static setter and getter examples in java
- Category
- Java
- Title
- how to declare an array in java
- Category
- Java
- Title
- java calculator code
- Category
- Java
- Title
- math max java
- Category
- Java
- Title
- hashmap get value by key java
- Category
- Java
- Title
- java xml element get attribute value
- Category
- Java
- Title
- kotlin add value to MutableLiveData
- Category
- Java
- Title
- Spigot API inventory close
- Category
- Java
- Title
- java random integer
- Category
- Java
- Title
- how to create a random number in java
- Category
- Java
- Title
- how to replace in java
- Category
- Java
- Title
- how to print array in java using for loop
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- main method java
- Category
- Java
- Title
- how to stop a void java
- Category
- Java
- Title
- diagonal difference hackerrank solution in java 8 using list
- Category
- Java
- Title
- java repository sql find not in list
- Category
- Java
- Title
- binary to int java
- Category
- Java
- Title
- intellij replace all
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java