sieve in java
Java
class SieveOfEratosthenes
{
void sieveOfEratosthenes(int n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
boolean prime[] = new boolean[n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for(int i = 2; i <= n; i++)
{
if(prime[i] == true)
System.out.print(i + " ");
}
}
// Driver Program to test above function
public static void main(String args[])
{
int n = 30;
System.out.print("Following are the prime numbers ");
System.out.println("smaller than or equal to " + n);
SieveOfEratosthenes g = new SieveOfEratosthenes();
g.sieveOfEratosthenes(n);
}
}
Also in Java:
- Title
- linux command to see all the java version installed
- Category
- Java
- Title
- android foreground push notification
- Category
- Java
- Title
- Java If statemtn
- Category
- Java
- Title
- summary of operator java
- Category
- Java
- Title
- how to create array of linkedlist in java
- Category
- Java
- Title
- insertion sort doubly linked list java
- Category
- Java
- Title
- number of matches regex java
- Category
- Java
- Title
- java singleton implementation
- Category
- Java
- Title
- java treemap
- Category
- Java
- Title
- how to push an element in hashset java
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- android cardview dependency
- Category
- Java
- Title
- java comments
- Category
- Java
- Title
- how to read a csv file java
- Category
- Java
- Title
- spring iterate
- Category
- Java
- Title
- how to do a linear searc in java
- Category
- Java
- Title
- how to copy all files and subdirectories in directory in java
- Category
- Java
- Title
- two pointer approach java
- Category
- Java
- Title
- how to declare a linked list in java
- Category
- Java
- Title
- Error executing Maven. java.io.FileNotFoundException: The specified user settings file does not exist: /etc/java-8-openjdk
- Category
- Java
- Title
- java string to integer
- Category
- Java
- Title
- what is enum in java
- Category
- Java
- Title
- list java oracle
- Category
- Java
- Title
- float to string java
- Category
- Java
- Title
- google map set default zoom android studio
- Category
- Java
- Title
- java how to convert string to int
- Category
- Java
- Title
- catch array out of bounds exception java
- Category
- Java
- Title
- has been compiled by a more recent version of the Java Runtime (class file version 56.0), this version of the Java Runtime only recognizes class file versions up to 55.0
- Category
- Java
- Title
- bigdecimal divide java
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- java infinitew recursion
- Category
- Java
- Title
- java nested loop
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- bootstrap button bigger
- Category
- Java
- Title
- joptionpane fonctionnement java
- Category
- Java
- Title
- sort array java
- Category
- Java
- Title
- java is power of 2
- Category
- Java
- Title
- how to import another class in java
- Category
- Java
- Title
- select photo from camera android
- Category
- Java
- Title
- tableau de classe java
- Category
- Java
- Title
- finding min and max from given number in java
- Category
- Java
- Title
- how to do for each in java
- Category
- Java
- Title
- java cmd install raspbian
- Category
- Java
- Title
- get host from request object java
- Category
- Java
- Title
- delete element from arraylist
- Category
- Java
- Title
- replaceall in java
- Category
- Java
- Title
- java integer compareto
- Category
- Java
- Title
- java function that returns the index of the largest value in an array
- Category
- Java
- Title
- double round java integer
- Category
- Java
- Title
- java stream order by property
- Category
- Java
- Title
- convert date to calendar java
- Category
- Java
- Title
- java find biggest number in array
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- deleting elements of an array in java
- Category
- Java
- Title
- get sha key android
- Category
- Java
- Title
- java list contains object with property
- Category
- Java
- Title
- Unhandled exception: java.lang.InterruptedException
- Category
- Java
- Title
- center textview programmatically android
- Category
- Java
- Title
- java jpa criteriabuilder in xample
- Category
- Java
- Title
- convert string to mayus java
- Category
- Java
- Title
- android BottomSheetDialogFragment not opening fully on landscape
- Category
- Java
- Title
- reentrantreadwritelock java
- Category
- Java
- Title
- log4j print stacktrace
- Category
- Java
- Title
- java list as parameter
- Category
- Java
- Title
- copy file with byte java
- Category
- Java
- Title
- priority queue in java
- Category
- Java
- Title
- tower of hanoi program in java using recursion
- Category
- Java
- Title
- how to exit a for loop in java
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- ndroid.support.v4.content.FileProvider
- Category
- Java
- Title
- convert java list to array
- Category
- Java
- Title
- how to use the this keyword in java
- Category
- Java
- Title
- java - get open ports
- Category
- Java
- Title
- android java convert double to 2 decimal places
- Category
- Java
- Title
- java find duplicates in array
- Category
- Java
- Title
- import classes from another project java
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- load contents of file into string java
- Category
- Java
- Title
- arraylist remove method java
- Category
- Java
- Title
- difference between print and println in java
- Category
- Java
- Title
- send variable intent
- Category
- Java
- Title
- transformez integer en string
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- primitive and non primitive data types in java
- Category
- Java
- Title
- java stream collect to string
- Category
- Java
- Title
- java 8 loop in map
- Category
- Java
- Title
- run java classpath command line
- Category
- Java
- Title
- scan a string using scanner class in java
- Category
- Java
- Title
- declare array java inline
- Category
- Java
- Title
- java creare costante
- Category
- Java
- Title
- java save string in text file
- Category
- Java
- Title
- java singleton
- Category
- Java
- Title
- string replace last character java
- Category
- Java
- Title
- changing double space to single in java script
- Category
- Java
- Title
- how to write a java for loop?
- Category
- Java
- Title
- java string array
- Category
- Java
- Title
- how to get individual words from a string in java
- Category
- Java
- Title
- android java retrofit offline cache
- Category
- Java
- Title
- check leap year in java
- Category
- Java
- Title
- java.sql.SQLException: Before start of result set
- Category
- Java