sieve-of-eratosthenes

How can I modify my Akka streams Prime sieve to exclude modulo checks for known primes?

雨燕双飞 提交于 2019-12-23 01:41:32
问题 I wrote a sieve using akka streams to find prime members of an arbitrary source of Int : object Sieve extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer(ActorMaterializerSettings(system)) implicit val ctx = implicitly[ExecutionContext](system.dispatcher) val NaturalNumbers = Source.fromIterator(() => Iterator.from(2)) val IsPrimeByEurithmethes: Flow[Int, Int, _] = Flow[Int].filter { case n: Int => (2 to Math.floor(Math.sqrt(n)).toInt).par.forall(n % _ != 0)

Why does function apply complain about long lists?

给你一囗甜甜゛ 提交于 2019-12-22 08:24:54
问题 As part of some Eulerian travails, I'm trying to code a Sieve of Eratosthenes with a factorization wheel. My code so far is: (defun ring (&rest content) "Returns a circular list containing the elements in content. The returned list starts with the first element of content." (setf (cdr (last content)) content)) (defun factorization-wheel (lst) "Returns a circular list containing a factorization wheel using the list of prime numbers in lst" (let ((circumference (apply #'* lst))) (loop for i

Sieve of Eratosthenes with Wheel Factorization

早过忘川 提交于 2019-12-21 20:53:09
问题 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

Generate primes from 1 to n, crashing for n > 300 million

∥☆過路亽.° 提交于 2019-12-21 17:32:27
问题 Any suggestions as to how I can get this program to work for n = 1 trillion (aside from making upgrades / buying a new computer)? The error is as follows: after building, the program being executing (the command line style output window pops up) and then quickly closes out, and I get the following error "ProjectPrimes.exe has stopped working (Windows is looking for a solution to this problem." I suspect this has to do with memory issues as I first encountered it at n = 20 million but that was

Why is this prime sieve implementation slower?

我的未来我决定 提交于 2019-12-21 12:18:32
问题 I was just experimenting a bit with (for me) a new programming language: clojure. And I wrote a quite naive 'sieve' implementation, which I then tried to optimise a bit. Strangely enough though (for me at least), the new implementation wasn't faster, but much slower... Can anybody provide some insight in why this is so much slower? I'm also interested in other tips in how to improve this algorithm... Best regards, Arnaud Gouder ; naive sieve. (defn sieve ([max] (sieve max (range 2 max) 2)) (

Sieve of Eratosthenes using precalculated primes

左心房为你撑大大i 提交于 2019-12-19 10:25:42
问题 I've all prime numbers that can be stored in 32bit unsigned int and I want to use them to generate some 64bit prime numbers . using trial division is too slow even with optimizations in logic and compilation. I'm trying to modify Sieve of Eratosthenes to work with the predefined list, as follow: in array A from 2 to 4294967291 in array B from 2^32 to X inc by 1 find C which is first multiple of current prime. from C mark and jump by current prime till X. go to 1. The problem is step 3 which

Factorizing a number in Python

↘锁芯ラ 提交于 2019-12-19 09:16:10
问题 Here's my code: def factorize(n): sieve = [True] * (n + 1) for x in range(2, int(len(sieve) ** 0.5) + 1): if sieve[x]: for i in range(x + x, len(sieve), x): sieve[i] = False lowerPrimes = i for i in range(2, len(sieve)) if sieve[i]] and (n % i == 0)] return lowerPrimes factorize(n) returns all prime factors of the given value n . As you can see, it first makes an Eratosthenes sieve for n and then uses a list comprehension to return all values in the sieve that are factors of n . It works

Factorizing a number in Python

夙愿已清 提交于 2019-12-19 09:16:05
问题 Here's my code: def factorize(n): sieve = [True] * (n + 1) for x in range(2, int(len(sieve) ** 0.5) + 1): if sieve[x]: for i in range(x + x, len(sieve), x): sieve[i] = False lowerPrimes = i for i in range(2, len(sieve)) if sieve[i]] and (n % i == 0)] return lowerPrimes factorize(n) returns all prime factors of the given value n . As you can see, it first makes an Eratosthenes sieve for n and then uses a list comprehension to return all values in the sieve that are factors of n . It works

Program to find all primes in a very large given range of integers

我的未来我决定 提交于 2019-12-19 07:54:11
问题 i came across this following question on a programming website : Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space. I came up with the following solution : import java.util.*; public class PRIME1 {

Porting optimized Sieve of Eratosthenes from Python to C++

陌路散爱 提交于 2019-12-18 17:56:38
问题 Some time ago I used the (blazing fast) primesieve in python that I found here: Fastest way to list all primes below N To be precise, this implementation: def primes2(n): """ Input n>=6, Returns a list of primes, 2 <= p < n """ n, correction = n-n%6+6, 2-(n%6>1) sieve = [True] * (n/3) for i in xrange(1,int(n**0.5)/3+1): if sieve[i]: k=3*i+1|1 sieve[ k*k/3 ::2*k] = [False] * ((n/6-k*k/6-1)/k+1) sieve[k*(k-2*(i&1)+4)/3::2*k] = [False] * ((n/6-k*(k-2*(i&1)+4)/6-1)/k+1) return [2,3] + [3*i+1|1