sieve-of-eratosthenes

Reducing memory usage when designing a sieve of eratosthenes in C

匆匆过客 提交于 2021-02-08 08:28:17
问题 I'm trying to design a sieve of eratosthenes in C but I've run into two strange problems which I can't figure out. Here's my basic program outline. Ask users to set a range to display primes from. If the range minimum is below 9, set the minimum as 9. Fill an array with all odd numbers in the range. 1) I'm trying to reduce memory usage by declaring variable size arrays like so: if (max<=UINT_MAX) unsigned int range[(max-min)/2]; else if (max<=ULONG_MAX) unsigned long int range[(max-min)/2];

Finding all Coprime subset upto a number N

旧巷老猫 提交于 2020-05-08 19:48:25
问题 Suppose I have numbers 1 to N and I want to divide them into subsets based on following criteria: Each number can be present in only 1 subset. The elements of the subsets must be mutually coprime. Minimizing the total number of subsets. My approach to it is by finding all primes up to N using Sieve of Eratosthenes and then dividing them accordingly in subsets. For example for N=5, I can have two subsets at minimum {1,2,3,5} and {4}. But I am unsure how to distribute the elements in subsets so

Understanding Sieve of Eratosthenes in Python

自作多情 提交于 2020-01-16 03:29:09
问题 I've found an example code in python that gives out all prime numbers upto n but I simply don't get it, Why does it does what it does? I've read the wikipedia article about the Sieve of Eratosthenes but simply have no idea about how this works. pp = 2 ps = [pp] lim = raw_input("Generate prime numbers up to what number? : ") while pp < int(lim): pp += 1 for a in ps: if pp%a==0: break else: ps.append(pp) print set(ps) An explanation of how the loop works would be appreciated. EDIT - Figured out

Scala performance - Sieve

我的梦境 提交于 2020-01-15 12:54:47
问题 Right now, I am trying to learn Scala . I've started small, writing some simple algorithms . I've encountered some problems when I wanted to implement the Sieve algorithm from finding all all prime numbers lower than a certain threshold . My implementation is: import scala.math object Sieve { // Returns all prime numbers until maxNum def getPrimes(maxNum : Int) = { def sieve(list: List[Int], stop : Int) : List[Int] = { list match { case Nil => Nil case h :: list if h <= stop => h :: sieve

Scala performance - Sieve

心已入冬 提交于 2020-01-15 12:53:27
问题 Right now, I am trying to learn Scala . I've started small, writing some simple algorithms . I've encountered some problems when I wanted to implement the Sieve algorithm from finding all all prime numbers lower than a certain threshold . My implementation is: import scala.math object Sieve { // Returns all prime numbers until maxNum def getPrimes(maxNum : Int) = { def sieve(list: List[Int], stop : Int) : List[Int] = { list match { case Nil => Nil case h :: list if h <= stop => h :: sieve

Sieve of Erathostenes ArrayIndexOutOfBounds

眉间皱痕 提交于 2020-01-15 09:42:28
问题 trying to implement a simple sieve of erathosthenes to solve this question on project euler : The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. Link My code keeps returning this error however : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147479015 at Prime.main(Prime.java:28) Can anyone give me any hints as to why? Here is the code: import java.math.BigInteger; public class Prime { /* * Input: an integer n > 1 *

Segmented Sieve of Atkin, possible?

て烟熏妆下的殇ゞ 提交于 2020-01-11 03:07:35
问题 I am aware of the fact that the Sieve of Eratosthenes can be implemented so that it finds primes continuosly without an upper bound (the segmented sieve). My question is, could the Sieve of Atkin/Bernstein be implemented in the same way? Related question: C#: How to make Sieve of Atkin incremental However the related question has only 1 answer, which says "It's impossible for all sieves", which is obviously incorrect. 回答1: Atkin/Bernstein give a segmented version in Section 5 of their