primes

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

Checking if a number is a prime number in Python [duplicate]

末鹿安然 提交于 2019-11-26 00:53:01
问题 This question already has an answer here: How to create the most compact mapping n → isprime(n) up to a limit N? 28 answers I have written the following code, which should check if the entered number is a prime number or not, but there is an issue i couldn\'t get through: def main(): n = input(\"Please enter a number:\") is_prime(n) def is_prime(a): x = True for i in (2, a): while x: if a%i == 0: x = False else: x = True if x: print \"prime\" else: print \"not prime\" main() If the entered

Why use a prime number in hashCode?

淺唱寂寞╮ 提交于 2019-11-26 00:14:06
问题 I was just wondering why is that primes are used in a class\'s hashCode() method? For example, when using Eclipse to generate my hashCode() method there is always the prime number 31 used: public int hashCode() { final int prime = 31; //... } References: Here is a good primer on Hashcode and article on how hashing works that I found (C# but the concepts are transferrable): Eric Lippert\'s Guidelines and rules for GetHashCode() 回答1: Because you want the number you are multiplying by and the

Simple Prime Generator in Python

拜拜、爱过 提交于 2019-11-26 00:12:53
问题 Could someone please tell me what I\'m doing wrong with this code? It is just printing \'count\' anyway. I just want a very simple prime generator (nothing fancy). import math def main(): count = 3 one = 1 while one == 1: for x in range(2, int(math.sqrt(count) + 1)): if count % x == 0: continue if count % x != 0: print count count += 1 回答1: There are some problems: Why do you print out count when it didn't divide by x? It doesn't mean it's prime, it means only that this particular x doesn't

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)?

Which is the fastest algorithm to find prime numbers?

隐身守侯 提交于 2019-11-25 23:17:40
问题 Which is the fastest algorithm to find out prime numbers using C++? I have used sieve\'s algorithm but I still want it to be faster! 回答1: A very fast implementation of the Sieve of Atkin is Dan Bernstein's primegen. This sieve is more efficient than the Sieve of Eratosthenes. His page has some benchmark information. 回答2: If it has to be really fast you can include a list of primes: http://www.bigprimes.net/archive/prime/ If you just have to know if a certain number is a prime number, there

How to create the most compact mapping n → isprime(n) up to a limit N?

假装没事ソ 提交于 2019-11-25 22:59:45
问题 Naturally, for bool isprime(number) there would be a data structure I could query. I define the best algorithm , to be the algorithm that produces a data structure with lowest memory consumption for the range (1, N], where N is a constant. Just an example of what I am looking for: I could represent every odd number with one bit e.g. for the given range of numbers (1, 10], starts at 3: 1110 The following dictionary can be squeezed more, right? I could eliminate multiples of five with some work

Why do we check up to the square root of a prime number to determine if it is prime?

旧巷老猫 提交于 2019-11-25 22:34:50
问题 To test whether a number is prime or not, why do we have to test whether it is divisible only up to the square root of that number? 回答1: If a number n is not a prime, it can be factored into two factors a and b : n = a * b If both a and b were greater than the square root of n , then a * b would be greater than n . So at least one of those factors must be less than or equal to the square root of n , and if we can't find any factors less than or equal to the square root, n must be prime. 回答2:

How to implement an efficient infinite generator of prime numbers in Python?

橙三吉。 提交于 2019-11-25 22:27:02
问题 This is not a homework, I am just curious. INFINITE is the key word here. I wish to use it as for p in primes() . I believe that this is a built-in function in Haskell. So, the answer cannot be as naive as \"Just do a Sieve\". First of all, you do not know how many consecutive primes will be consumed. Well, suppose you could concoct 100 of them at a time. Would you use the same Sieve approach as well as the frequency of prime numbers formula? I prefer non-concurrent approach. Thank you for

Fastest way to list all primes below N

て烟熏妆下的殇ゞ 提交于 2019-11-25 22:14:13
问题 This is the best algorithm I could come up. def get_primes(n): numbers = set(range(n, 1, -1)) primes = [] while numbers: p = numbers.pop() primes.append(p) numbers.difference_update(set(range(p*2, n+1, p))) return primes >>> timeit.Timer(stmt=\'get_primes.get_primes(1000000)\', setup=\'import get_primes\').timeit(1) 1.1499958793645562 Can it be made even faster? This code has a flaw: Since numbers is an unordered set, there is no guarantee that numbers.pop() will remove the lowest number from