sieve-of-eratosthenes

Java implementation of Sieve of Eratosthenes that can go past n = 2^32?

我们两清 提交于 2019-12-05 04:18:01
Currently I have this prime generator that is limited to n < 2^32-1. I'm not entirely sure how I could expand the limit further, given the limit of elements in an array. Sieve: public class Main { public static void main(String args[]){ long N = 2000000000; // initially assume all integers are prime boolean[] isPrime = new boolean[N + 1]; for (int i = 2; i <= N; i++) { isPrime[i] = true; } // mark non-primes <= N using Sieve of Eratosthenes for (int i = 2; i*i <= N; i++) { // if i is prime, then mark multiples of i as nonprime // suffices to consider mutiples i, i+1, ..., N/i if (isPrime[i]) {

Linear time Euler's Totient Function calculation

牧云@^-^@ 提交于 2019-12-04 18:36:30
After surfing a bit ,I found this code for calculating Euler's phi values in linear time using Sieve of Eratostenes .But failed to understand the main logic used in this code,specially what is done in the inner for loop and idea used in this loop for calculating phi value.It will be helpful if someone helps me understand this code. #define MAXN 3000000 int phi[MAXN + 1], prime[MAXN/10], sz; bitset <MAXN + 1> mark; for (int i = 2; i <= MAXN; i++ ){ if(!mark[i]){ phi[i] = i-1; prime[sz++]= i; } for (int j=0; j<sz && prime[j]*i <= MAXN; j++ ){ mark[prime[j]*i]=1; if(i%prime[j]==0){ phi[i*prime[j]

Sieve of Eratosthenes has huge 'overdraw' - is Sundaram's better after all?

蹲街弑〆低调 提交于 2019-12-04 12:38:22
The standard Sieve of Eratosthenes crosses out most composites multiple times; in fact the only ones that do not get marked more than once are those that are the product of exactly two primes. Naturally, the overdraw increases as the sieve gets bigger. For an odd sieve (i.e. without the evens) the overdraw hits 100% for n = 3,509,227, with 1,503,868 composites and 1,503,868 crossings-out of already crossed-out numbers. For n = 2^32 the overdraw rises to 134.25% (overdraw 2,610,022,328 vs. pop count 1,944,203,427 = (2^32 / 2) - 203,280,221). The Sieve of Sundaram - with another explanation at

Sieve of Eratosthenes with Wheel Factorization

江枫思渺然 提交于 2019-12-04 12:27:45
i'm implementing a reasonably fast prime number generator and i obtained some nice results with a few optimizations on the sieve of eratosthenes. In particular, during the preliminary part of the algorithm, i skip all multiples of 2 and 3 in this way: template<class Sieve, class SizeT> void PrimeGenerator<Sieve, SizeT>::factorize() { SizeT c = 2; m_sieve[2] = 1; m_sieve[3] = 1; for (SizeT i=5; i<m_size; i += c, c = 6 - c) m_sieve[i] = 1; } Here m_sieve is a boolean array according to the sieve of eratosthenes. I think this is a sort of Wheel factorization only considering primes 2 and 3,

No of Pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion

纵然是瞬间 提交于 2019-12-04 11:37:11
问题 How to find number of pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion (using any programming language and without using any external libraries) with considering time complexity? Tried sieve of eratosthenes but getting consecutive primes is challenge Used generators but time complexity is very high The code is: def gen_numbers(n): for ele in range(1,n+1): for i in range(2,ele//2): if ele%i==0: break else: yield ele prev=0 count=0 for i in gen_numbers

Is there a fast, functional prime generator?

假装没事ソ 提交于 2019-12-04 03:41:06
Suppose I've got a natural number n and I want a list (or whatever) of all primes up to n . The classic prime sieve algorithm runs in O(n log n) time and O(n) space -- it's fine for more imperative languages, but requires in-place modification to lists and random access, in a fundamental way. There's a functional version involving priority queues, which is pretty slick -- you can check it out here . This has better space complexity at about O(n / log(n)) (asymptotically better but debatable at practical scales). Unfortunately the time analysis is nasty, but it's very nearly O(n^2) (actually, I

CUDA - Sieve of Eratosthenes division into parts

≡放荡痞女 提交于 2019-12-03 21:18:18
I'm writing implementation of Sieve of Eratosthenes ( https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes ) on GPU. But no sth like this - http://developer-resource.blogspot.com/2008/07/cuda-sieve-of-eratosthenes.html Method: Creating n-element array with default values 0/1 (0 - prime, 1 - no) and passing it on GPU (I know that it can be done directly in kernel but it's not problem in this moment). Each thread in block checks multiples of a single number. Each block checks in total sqrt(n) possibilities. Each block == different interval. Marking multiples as 1 and passing data back to the host

No of Pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion

大兔子大兔子 提交于 2019-12-03 07:11:23
How to find number of pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion (using any programming language and without using any external libraries) with considering time complexity? Tried sieve of eratosthenes but getting consecutive primes is challenge Used generators but time complexity is very high The code is: def gen_numbers(n): for ele in range(1,n+1): for i in range(2,ele//2): if ele%i==0: break else: yield ele prev=0 count=0 for i in gen_numbers(2000000000): if i-prev==6: count+=1 prev = i Interesting question! I have recently been working on

Sieve of Eratosthenes in Python

我的未来我决定 提交于 2019-12-02 17:02:19
问题 I am trying to write a python function to return the number of primes less than a given value and the values of all the primes. I need to use the Sieve of Eratosthenes algorithm. I believe I'm missing something in the function - For example, when I want to find the primes under 100. All I got is 2, 3, 5, 7. I am aware that if I don't use "square root", I can get all the primes I need; but I am told that I need to include square root there. Can someone please take a look at my code and let me

Sieve of Eratosthenes without arrays?

僤鯓⒐⒋嵵緔 提交于 2019-12-02 13:23:28
问题 I have to write a java code for the 'sieve of eratosthenes' algorithm to print out primes up to a given max value on the console but I'm not allowed to use arrays. Our professor told us it is possible to do only with the help of loops. So I thought a lot and googled a lot about this topic and couldn't find an answer. I dont think it's possible at all because you have store the information which digits are already crossed out somewhere. my code until now: public static void main(String[] args)