Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

prime number between given range in java

/* Solution below implements the Sieve of Eratosthenes algorithm
   https://www.geeksforgeeks.org/sieve-of-eratosthenes/
   Space complexity: O(upper)
   Time complexity: O(upper*log(log(upper)))
*/

import java.util.*;
public List<Integer> primesInRange(int lower, int upper) {
	if (lower > upper || upper <= 2) {
    	return new ArrayList<>();
    }
    List<Integer> result = new ArrayList<>();
    boolean[] primes = new boolean[upper];
    for (int p = lower; p <= (int)Math.sqrt(upper); ++p) {
    	if (primes[p] == false) {
        	for (int scan = p*p; scan < upper; scan += p) {
            	primes[scan] = true;
             }
         }
    }
        
	for (int idx = lower; idx < upper; idx++) {
    	if (primes[idx] == false) {
        	result.add(idx);
         }
     }
        
	return result;
}
Comment

PREVIOUS NEXT
Code Example
Java :: loop will array length java example 
Java :: java transfer file over socket 
Java :: kill all java processes linux 
Java :: recyclerview stop scrolling 
Java :: java int to biginteger 
Java :: random word java 
Java :: rps java 
Java :: java find longest string in list 
Java :: conditions in for loop java 
Java :: sort list of string lexicographically java 
Java :: java expressions 
Java :: Java String to ByteBuffer conversion 
Java :: How do I make a splash screen? 
Java :: android activity read intent extra 
Java :: what language is android written in 
Java :: len of number in java 
Java :: get username from jwt token request spring boot 
Java :: priority queue java comparator lambda 
Java :: java scanner netLine 
Java :: How to solve the N queens problem in Java? 
Java :: java convert float to int 
Java :: check whether a double is not a number in java 
Java :: how to detect device javascirpt 
Java :: calculator with java 
Java :: upload converted base64 to image object to oss without saving in java 
Java :: 12 hours to 24 hours java 
Java :: Java how to make a number without a digit 
Java :: how to get last element of array java 
Java :: remove last <br/ from string java 
Java :: explicit casting 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =