sieve-of-eratosthenes

Sieve of Eratosthenes - Finding Primes Python

僤鯓⒐⒋嵵緔 提交于 2019-11-26 01:20:23
问题 Just to clarify, this is not a homework problem :) I wanted to find primes for a math application I am building & came across Sieve of Eratosthenes approach. I have written an implementation of it in Python. But it\'s terribly slow. For say, if I want to find all primes less than 2 million. It takes > 20 mins. (I stopped it at this point). How can I speed this up? def primes_sieve(limit): limitn = limit+1 primes = range(2, limitn) for i in primes: factors = range(i, limitn, i) for f in

Segmented Sieve of Eratosthenes?

点点圈 提交于 2019-11-25 23:40:15
问题 It\'s easy enough to make a simple sieve: for (int i=2; i<=N; i++){ if (sieve[i]==0){ cout << i << \" is prime\" << endl; for (int j = i; j<=N; j+=i){ sieve[j]=1; } } cout << i << \" has \" << sieve[i] << \" distinct prime factors\\n\"; } But what about when N is very large and I can\'t hold that kind of array in memory? I\'ve looked up segmented sieve approaches and they seem to involve finding primes up until sqrt(N) but I don\'t understand how it works. What if N is very large (say 10^18)?

Program to find prime numbers

吃可爱长大的小学妹 提交于 2019-11-25 19:17:32
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; } } static void Main(string[] args) { Program p = new Program(); p.prime_num (999999999999999L); Console