sieve-of-eratosthenes

Python Eratosthenes Sieve Algorithm Optimization

久未见 提交于 2019-12-18 09:17:45
问题 I'm attempting to implement the Sieve of Eratosthenes. The output seems to be correct (minus "2" that needs to be added) but if the input to the function is larger than 100k or so it seems to take an inordinate amount of time. What are ways that I can optimize this function? def sieveErato(n): numberList = range(3,n,2) for item in range(int(math.sqrt(len(numberList)))): divisor = numberList[item] for thing in numberList: if(thing % divisor == 0) and thing != divisor: numberList.remove(thing)

Finding prime numbers with the Sieve of Eratosthenes (Originally: Is there a better way to prepare this array?)

杀马特。学长 韩版系。学妹 提交于 2019-12-17 10:41:55
问题 Note: Version 2, below, uses the Sieve of Eratosthenes. There are several answers that helped with what I originally asked. I have chosen the Sieve of Eratosthenes method, implemented it, and changed the question title and tags appropriately. Thanks to everyone who helped! Introduction I wrote this fancy little method that generates an array of int containing the prime numbers less than the specified upper bound. It works very well, but I have a concern. The Method private static int []

Time complexity of Sieve of Eratosthenes algorithm

瘦欲@ 提交于 2019-12-17 05:34:30
问题 From Wikipedia: The complexity of the algorithm is O(n(logn)(loglogn)) bit operations. How do you arrive at that? That the complexity includes the loglogn term tells me that there is a sqrt(n) somewhere. Suppose I am running the sieve on the first 100 numbers ( n = 100 ), assuming that marking the numbers as composite takes constant time (array implementation), the number of times we use mark_composite() would be something like n/2 + n/3 + n/5 + n/7 + ... + n/97 = O(n^2) And to find the next

double stream feed to prevent unneeded memoization?

假如想象 提交于 2019-12-17 05:14:14
问题 I'm new to Haskell and I'm trying to implement Euler's Sieve in stream processing style. When I checked the Haskell Wiki page about prime numbers, I found some mysterious optimization technique for streams. In 3.8 Linear merging of that wiki: primesLME = 2 : ([3,5..] `minus` joinL [[p*p, p*p+2*p..] | p <- primes']) where primes' = 3 : ([5,7..] `minus` joinL [[p*p, p*p+2*p..] | p <- primes']) joinL ((x:xs):t) = x : union xs (joinL t) And it says “ The double primes feed is introduced here to

double stream feed to prevent unneeded memoization?

丶灬走出姿态 提交于 2019-12-17 05:13:32
问题 I'm new to Haskell and I'm trying to implement Euler's Sieve in stream processing style. When I checked the Haskell Wiki page about prime numbers, I found some mysterious optimization technique for streams. In 3.8 Linear merging of that wiki: primesLME = 2 : ([3,5..] `minus` joinL [[p*p, p*p+2*p..] | p <- primes']) where primes' = 3 : ([5,7..] `minus` joinL [[p*p, p*p+2*p..] | p <- primes']) joinL ((x:xs):t) = x : union xs (joinL t) And it says “ The double primes feed is introduced here to

Is there a fast, functional prime generator?

荒凉一梦 提交于 2019-12-14 00:22:23
问题 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

Prime numbers in c language

怎甘沉沦 提交于 2019-12-13 11:21:59
问题 I want to find prime numbers with multithreading and using Sieve of E. function.I write some piece of codes. If the program will run, the user enter a max number and thread number. The program should create threads that given thread number. The program find all prime numbers until the max number. Each thread must check one prime number. My program doesn't find prime numbers. I write checkPrime function and crossout functions for finding prime numbers efficiently. But it doesn't work. So, I

Parallel sieve of Eratosthenes - Java Multithreading

╄→гoц情女王★ 提交于 2019-12-12 19:14:10
问题 I wanted to write sieve of Eratosthenes which will work using specific number of threads. I figured out, that it will work in following way: For 2 threads up to 17. Thread-1 takes 2, and starts to remove multiple of 2 from List. Parallel Thread-2 takes 3 and does the same. After that Thread-1 takes 5( because there is no 4 in List) and Thread-2 takes 7 and so on until they reach end. I wrote following piece of code: private List<Integer> array = new ArrayList<Integer>(); private List<Integer>

How does segmentation improve the running time of Sieve of Eratosthenes?

て烟熏妆下的殇ゞ 提交于 2019-12-12 16:58:01
问题 I came across a segmented implementation of sieve of Eratosthenes which promises to run many times faster than the conventional version. Can someone please explain how segmentation improves the running time? Note that I want to find prime numbers in [1,b] . It works on this idea: (for finding prime numbers till 10^9) We first generate the sieving primes below sqrt(10^9) which are needed to cross-off multiples. Then we start crossing-off the multiples of the first prime 2 until we reach a

Sieve of Eratosthenes infinite list

两盒软妹~` 提交于 2019-12-12 11:37:52
问题 Hello i have to implement a function for sieve of Eratosthenes. I already have a function removep p l that removes elements of l that match the predicate p and a function nats that returns an infinite list of naturals numbers, and i am supposed to use both in my solution. Now, i do understand how the sieve itself works but it seems i am having problems implementing this. I am doing something like this: sieve = (drop 1 nats) where sieve (h:t) = h : (removep (\x -> (mod x p) == 0) t) : sieve