sieve_of_eratosthenes | python function for finding prime numbers
kandi X-RAY | sieve_of_eratosthenes Summary
kandi X-RAY | sieve_of_eratosthenes Summary
A python function for finding prime numbers using the Sieve of Eratosthenes algorithm
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Sieve sieve sieve .
- Convert a number to index .
- Convert an index to a number .
sieve_of_eratosthenes Key Features
sieve_of_eratosthenes Examples and Code Snippets
Community Discussions
Trending Discussions on sieve_of_eratosthenes
QUESTION
I have been trying to create a C++ program to find all the prime numbers under a certain integer, n, using the Sieve of Eratosthenes algorithm, which goes as follows:
Create a vector of integers ranging from 2 to n.
Start with the smallest element of the vector, 2, add it to a new vector of primes, and remove all multiples of 2 from the integers vector.
Repeat this process with the new smallest element of the integers vector (i.e. 3) and repeat until the integers vector is empty. The primes vector then contains all the required primes.
Now, this code works:
...ANSWER
Answered 2021-Aug-21 at 22:19std::remove_if()
(potentially) re-orders the elements of the vector. Thus, ptr
no longer necessarily points to the same value in further calls of the std::remove_if()
predicate. By contrast, the value of p
is not affected by std::remove_if()
.
You can see that they aren't the same with a little bit of ad-hoc debugging:
QUESTION
This is a question about Python internals. The following code is taken from this video about laziness in python:
...ANSWER
Answered 2021-Aug-14 at 20:10clearly some preservation of a context of variables is necessary for each layer pass, as each layer "sees" a different
n
for example.
Yes, this is not specific to generators, but to any function call: if that function calls a function (possibly itself), then its local variables are preserved in a stack frame, and the new function execution context gets its own set of local variables.
Is it adding a stack frame for each nested generator call?
Yes. So in the case of sieve
, each execution context of sieve
has its own n
and s
variables.
In the expression that sieve
passes to the recursive call, it is creating a new, more restrictive, iterator from the existing one it got as argument. We could work backwards to see what the complete iterator looks like.
The first recursive call, can be expanded to:
QUESTION
Im currently trying to implement a version of the sieve of eratosthenes for a Kattis problem, however, I am running into some memory constraints that my implementation wont pass.
Here is a link to the problem statement. In short the problem wants me to first return the amount of primes less or equal to n and then solve for a certain number of queries if a number i is a prime or not. There is a constraint of 50 MB memory usage as well as only using the standard libraries of python (no numpy etc). The memory constraint is where I am stuck.
Here is my code so far:
...ANSWER
Answered 2020-Jul-14 at 16:40I think you can try by using a list of booleans to mark whether its index is prime or not:
QUESTION
I have a function that It has to be in size_t*
type. When all size_t
types are declared as integers, all it works perfect. When I change the decleration to size_t
it doesn't run with this fail Aborted (core dumped)
. If i change all size_t*
to int
no problem happens.
That's the function:
...ANSWER
Answered 2020-Nov-09 at 08:19Consider your lines
QUESTION
I'm trying to write a program in prolog that finds all prime numbers up to a limit N
, I'm trying to achieve this by using the Sieve of Eratosthenes. I'm very new to prolog so I haven't really mastered the art of thinking recursively (you can probably see that in my code).
Nonetheless, I (more or less) tried implementing the algorithm in prolog, but didn't get very far as you'll see here:
...ANSWER
Answered 2020-Oct-06 at 22:02You cannot change the list of primes after you instantiated it. You may however further instantiate some items on it if they were uninstantiated (not your case) and you could probably solve this problem that way.
Here goes a recursive solution based on your algorithm:
QUESTION
I wanted to code a prime number generator in python - I've only done this in C and Java. I did the following. I used an integer bitmap as an array. Performance of the algorithm should increase nlog(log(n))
but I am seeing exponential increase in cost/time as the problem size n
increases. Is this something obvious I am not seeing or don't know about python as integers grow larger than practical? I am using python-3.8.3.
ANSWER
Answered 2020-Jul-14 at 23:14I used an integer bitmap as an array
That's extremely expensive. Python ints are immutable. Every time you want to toggle a bit, you're building a whole new gigantic int.
You also need to build other giant ints just to access single bits you're interested in - for example, composite
and ~composite
are huge in arr = arr & (~composite)
, even though you're only interested in 1 bit.
Use an actual mutable sequence type. Maybe a list, maybe a NumPy array, maybe some bitvector type off of PyPI, but don't use an int
.
QUESTION
I'm playing around with the multiprocessing module in python and trying to parallelize an algorithm that loops through an list with a different increment value each time (modification of the Sieve of Eratosthenes algorithm). Therefore, I want to have a shared list between all of the processes so that all the processes are modifying the same list. I've tried with the multiprocessing.Array
function, but when I reach the end of the program the array is still unmodified and still contains all 0's (the value that I initialized it to).
ANSWER
Answered 2020-Jul-03 at 13:33You could try to use multiprocessing.Manager for your task:
QUESTION
I am creating a sieve of Eratosthenes in order to sum more efficiently the prime numbers between 1 and a large number n. What I want to do is to create a list from 2 to n, and then remove the multiples of 2, then the multiples of 3, then the multiples of the next number in the list, and so on. The code I have created I think it has very slow performance in time, it is almost like creating a list by checking if each entry is a prime number. I guess the number of operations that I have is of order: square root of n (the first while loop) times (a bit less than) square root of n (for the second while loop). So I am not sure if the remove method or maybe other thing is slowing it down.
My code is this one:
...ANSWER
Answered 2020-Apr-17 at 23:35Assumption
The question is on how to improve the run time of your software since it's very slow.`
Performed following two code changes to speed up your code
- Rather than keeping a list of primes, check numbers as Prime (True) or non-Prime (False)
- Check only odd numbers > 2 for prime
Code
QUESTION
I wrote two prime finder functions and the sieve only performs about 10% better. I'm using two optimizations for the simple version.
- Don't check even numbers
- Only check up to the square root or
j * j <= i
. ( equivalent )
and one optimization for the sieve version
- Only check up to the square root or
i * i <= n
. ( equivalent )
What optimizations can I add to the sieve?
My sieve is pretty slow. I don't want to do a bitwise implementation yet, I want to understand if this implementation offers any benefits.
Or if I missed an implementation point.
The inner for
loop in the pseudocode here looks interesting / odd
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
I don't know how to interpret it. (update: the OP seems to indicate in the comments that it was an issue with incorrect formatting after copy-pasting the pseudocode from Wikipedia, and with the corrected formatting it is clear now)
Here it is:
algorithm Sieve of Eratosthenes is:
...
ANSWER
Answered 2020-Mar-03 at 06:34What you see is an expression of the differences in theoretical run time complexities, i.e. the true algorithmic differences between the two algorithms.
Optimal trial division sieve's complexity is O(n1.5/(log n)2)(*) whereas the sieve of Eratosthenes' complexity is O(n log log n).
According to the empirical run time figures posted by Scott Sauyet in the comments,
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install sieve_of_eratosthenes
You can use sieve_of_eratosthenes like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page