primes

C++ Sieve of Atkin overlooks a few prime numbers

无人久伴 提交于 2019-12-04 13:43:15
Recently I've been working on a C++ prime generator that uses the Sieve of Atkin ( http://en.wikipedia.org/wiki/Sieve_of_atkin ) to generate its primes. My objective is to be able to generate any 32-bit number. I'll use it mostly for project euler problems. mostly it's just a summer project. The program uses a bitboard to store primality: that is, a series of ones and zeros where for example the 11th bit would be a 1, the 12th a 0, and the 13th a 1, etc. For efficient memory usage, this is actually and array of chars, each char containing 8 bits. I use flags and bitwise-operators to set and

Python Beginner's Loop (Finding Primes)

最后都变了- 提交于 2019-12-04 13:32:47
问题 I'm truly a beginner at python so I apologise for the lack of knowledge, but the reason I'm asking is that reading the Python manual and tutorial (http://docs.python.org/2.7/tutorial) I'm not unable to totally grasp how loops work. I've written some simple programs so I think I get the basics but for whatever reason this program that is meant to list all primes less than or equal to n is not working: n = int(raw_input("What number should I go up to? ")) p = 2 while p <= n: for i in range(2, p

Miller-Rabin Primality test in Java

本小妞迷上赌 提交于 2019-12-04 12:51:16
I am currently working on Project Euler and thought that it might be more interesting (and a better learning experience) if don't just brute force all of the questions. On question 3 it asks for prime factors of a number and my solution will be to factor the number (using another factoring algorithm) and then test the factors for primality. I came up with this code for a Miller-Rabin Primality test (after thoroughly researching primality test) and it returns true for all the composite odd number I have put in. Can anybody help me to figure out why? I thought I had coded the algorithm correctly

Sieve of Eratosthenes has huge 'overdraw' - is Sundaram's better after all?

蹲街弑〆低调 提交于 2019-12-04 12:38:22
The standard Sieve of Eratosthenes crosses out most composites multiple times; in fact the only ones that do not get marked more than once are those that are the product of exactly two primes. Naturally, the overdraw increases as the sieve gets bigger. For an odd sieve (i.e. without the evens) the overdraw hits 100% for n = 3,509,227, with 1,503,868 composites and 1,503,868 crossings-out of already crossed-out numbers. For n = 2^32 the overdraw rises to 134.25% (overdraw 2,610,022,328 vs. pop count 1,944,203,427 = (2^32 / 2) - 203,280,221). The Sieve of Sundaram - with another explanation at

2-3-5-7 wheel factorization seems to skip prime number 331

这一生的挚爱 提交于 2019-12-04 12:37:00
When following the procedure on wikipedia for wheel factorization , I seem to have stumbled into a problem where the prime number 331 is treated as a composite number if I try to build a 2-3-5-7 wheel. With 2-3-5-7 wheel, 2*3*5*7=210. So I setup a circle with 210 slots and go through steps 1-7 without any issues. Then I get to step 8 and strike off the spokes of all multiples of prime numbers, I eventually strike off the spoke rooted at 121, which is a multiple of 11, which is a prime. For the spoke rooted at 121, 121 + 210 = 331. Unfortunately, 331 is a prime number. Is the procedure on

Sieve of Eratosthenes with Wheel Factorization

江枫思渺然 提交于 2019-12-04 12:27:45
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 sieve of eratosthenes. I think this is a sort of Wheel factorization only considering primes 2 and 3,

Explain a code to check primality based on Fermat's little theorem

我的梦境 提交于 2019-12-04 12:07:20
I found some Python code that claims checking primality based on Fermat's little theorem : def CheckIfProbablyPrime(x): return (2 << x - 2) % x == 1 My questions: How does it work? What's its relation to Fermat's little theorem? How accurate is this method? If it's not accurate, what's the advantage of using it? I found it here . Dan 1. How does it work? Fermat's little theorem says that if a number x is prime, then for any integer a : If we divide both sides by a , then we can re-write the equation as follows: I'm going to punt on proving how this works (your first question) because there are

Checksumming large swathes of prime numbers? (for verification)

纵然是瞬间 提交于 2019-12-04 12:04:45
Are there any clever algorithms for computing high-quality checksums on millions or billions of prime numbers? I.e. with maximum error-detection capability and perhaps segmentable? Motivation: Small primes - up to 64 bits in size - can be sieved on demand to the tune of millions per second, by using a small bitmap for sieving potential factors (up to 2^32-1) and a second bitmap for sieving the numbers in the target range. Algorithm and implementation are reasonably simple and straightforward but the devil is in the details: values tend to push against - or exceed - the limits of builtin

Optimization of prime number code

≡放荡痞女 提交于 2019-12-04 11:50:39
This is my code in python for calculation of sum of prime numbers less than a given number. What more can I do to optimize it? import math primes = [2,] #primes store the prime numbers for i in xrange(3,20000,2): #i is the test number x = math.sqrt(i) isprime = True for j in primes: #j is the devider. only primes are used as deviders if j <= x: if i%j == 0: isprime = False break if isprime: primes.append(i,) print sum (primes,) You can use a different algorithm called the Sieve of Eratosthenes which will be faster but take more memory. Keep an array of flags, signifying whether each number is

No of Pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion

纵然是瞬间 提交于 2019-12-04 11:37:11
问题 How to find number of pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion (using any programming language and without using any external libraries) with considering time complexity? Tried sieve of eratosthenes but getting consecutive primes is challenge Used generators but time complexity is very high The code is: def gen_numbers(n): for ele in range(1,n+1): for i in range(2,ele//2): if ele%i==0: break else: yield ele prev=0 count=0 for i in gen_numbers