sieve-of-eratosthenes

Time complexity of Sieve of Eratosthenes algorithm

房东的猫 提交于 2019-11-26 21:25:15
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 prime number (for example to jump to 7 after crossing out all the numbers that are multiples of 5 ),

Speed up bitstring/bit operations in Python?

有些话、适合烂在心里 提交于 2019-11-26 19:43:51
I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The code runs correctly and gracefully at 0.32 seconds on ideone.com to generate prime numbers up to 1,000,000. # from bitstring import BitString def prime_numbers(limit=1000000): '''Prime number generator. Yields the series 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ... using Sieve of Eratosthenes. ''' yield 2 sub_limit = int(limit**0.5) flags = [False, False] + [True] * (limit - 2) # flags = BitString(limit) # Step through all the odd numbers for i in range(3, limit, 2): if flags[i] is False: # if flags[i] is True: continue

Find n primes after a given prime number, without using any function that checks for primality

╄→гoц情女王★ 提交于 2019-11-26 19:07:43
How to write a Program to find n primes after a given number? e.g. first 10 primes after 100, or first 25 primes after 1000. Edited: below is what I tried. I am getting output that way, but can we do it without using any primality-testing function? #include<stdio.h> #include<conio.h> int isprime(int); main() { int count=0,i; for(i=100;1<2;i++) { if(isprime(i)) { printf("%d\n",i); count++; if(count==5) break; } } getch(); } int isprime(int i) { int c=0,n; for(n=1;n<=i/2;n++) { if(i%n==0) c++; } if(c==1) return 1; else return 0; } Sure. Read about the Sieve of Eratosthenes . Instead of checking

The sieve of Eratosthenes in F#

余生颓废 提交于 2019-11-26 17:31:20
问题 I am interested in an implementation of the sieve of eratosthenes in purely functional F#. I am interested in an implementation of the actual sieve, not the naive functional implementation that isn't really the sieve, so not something like this: let rec PseudoSieve list = match list with | hd::tl -> hd :: (PseudoSieve <| List.filter (fun x -> x % hd <> 0) tl) | [] -> [] The second link above briefly describes an algorithm that would require the use of a multimap, which isn't available in F#

Sieve of Eratosthenes algorithm in JavaScript running endless for large number

橙三吉。 提交于 2019-11-26 15:49:19
问题 I have been trying to write Sieve of Eratosthenes algorithm in JavaScript. Basically I just literally followed the steps below: Create a list of consecutive integers from 2 to (n-1) Let first prime number p equal 2 Starting from p, count up in increments of p and removes each of these numbers (p and multiples of p) Go to the next number in the list and repeat 2,3,4 Add unintentionally deleted prime numbers back to the list and this is what I have come up with: function eratosthenes(n){ var

Adding wheel factorization to an indefinite sieve

♀尐吖头ヾ 提交于 2019-11-26 10:02:20
问题 I’m modifying an indefinite sieve of Eratosthenes from here so it uses wheel factorization to skip more composites than its current form of just checking all odds. I’ve worked out how to generate the steps to take to reach all the gaps along the wheel. From there I figured I could just substitute the +2’s for these wheel steps but it’s causing the sieve to skip primes. Here\'s the code: from itertools import count, cycle def dvprm(end): \"finds primes by trial division. returns a list\"

Speed up bitstring/bit operations in Python?

无人久伴 提交于 2019-11-26 07:23:58
问题 I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The code runs correctly and gracefully at 0.32 seconds on ideone.com to generate prime numbers up to 1,000,000. # from bitstring import BitString def prime_numbers(limit=1000000): \'\'\'Prime number generator. Yields the series 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ... using Sieve of Eratosthenes. \'\'\' yield 2 sub_limit = int(limit**0.5) flags = [False, False] + [True] * (limit - 2) # flags = BitString(limit) # Step

Find n primes after a given prime number, without using any function that checks for primality

此生再无相见时 提交于 2019-11-26 06:48:12
问题 How to write a Program to find n primes after a given number? e.g. first 10 primes after 100, or first 25 primes after 1000. Edited: below is what I tried. I am getting output that way, but can we do it without using any primality-testing function? #include<stdio.h> #include<conio.h> int isprime(int); main() { int count=0,i; for(i=100;1<2;i++) { if(isprime(i)) { printf(\"%d\\n\",i); count++; if(count==5) break; } } getch(); } int isprime(int i) { int c=0,n; for(n=1;n<=i/2;n++) { if(i%n==0) c+

Program to find prime numbers

旧街凉风 提交于 2019-11-26 03:21:37
问题 I want to find the prime number between 0 and a long variable but I am not able to get any output. The program is using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication16 { class Program { void prime_num(long num) { bool isPrime = true; for (int i = 0; i <= num; i++) { for (int j = 2; j <= num; j++) { if (i != j && i % j == 0) { isPrime = false; break; } } if (isPrime) { Console.WriteLine ( \"Prime:\" + i ); } isPrime = true; } }

Prime numbers by Eratosthenes quicker sequential than concurrently?

自闭症网瘾萝莉.ら 提交于 2019-11-26 02:03:36
问题 I am currently writing a program which first generates prime numbers by the Sieve of Eratosthenes sequentially, then concurrently. The concurrent version of the algorithm is supposed to be quicker than the sequential one, but in my case the concurrent version is approx. 10 times slower. I am wondering where I am putting the extra work on my threads, compared to the main thread in the sequential solution. Here\'s my program (prepare to read a bit!): Primes.java : public abstract class Primes {