Art_of_Programming_Contest_Part6 - CHAPTER 7 MATHEMATICS...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 7 MATHEMATICS 103 2. Pull out the sqrt call The first optimization was to pull the sqrt call out of the limit test, just in case the compiler wasn't optimizing that correctly, this one is faster: int is_prime(int n) { long lim = (int) sqrt(n); for (int i=2; i<=lim; i++) if (n%i == 0) return 0; return 1;} 3. Restatement of sqrt. int is_prime(int n) { for (int i=2; i*i<=n; i++) if (n%i == 0) return 0; return 1; } 4. We don't need to check even numbers int is_prime(int n) { if (n == 1) return 0; // 1 is NOT a prime if (n == 2) return 1; // 2 is a prime if (n%2 == 0) return 0; // NO prime is EVEN, except 2 for (int i=3; i*i<=n; i+=2) // start from 3, jump 2 numbers if (n%i == 0) // no need to check even numbers return 0; return 1; } 5. Other prime properties A (very) little bit of thought should tell you that no prime can end in 0,2,4,5,6, or 8, leaving only 1,3,7, and 9. It's fast & easy. Memorize this technique. It'll be very helpful for your programming assignments dealing with relatively small prime numbers (16-bit integer 1-32767). This divisibility check (step 1 to 5) will not be suitable for bigger numbers. First prime and the only even prime: 2.Largest prime in 32-bit integer range: 2^31 - 1 = 2,147,483,647 6. Divisibility check using smaller primes below sqrt(N): Actually, we can improve divisibility check for bigger numbers. Further investigation concludes that a number N is a prime if and only if no primes below sqrt(N) can divide N.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CHAPTER 7 MATHEMATICS 104 How to do this ? 1. Create a large array. How large? 2. Suppose max primes generated will not greater than 2^31-1 (2,147,483,647), maximum 32-bit integer. 3. Since you need smaller primes below sqrt(N), you only need to store primes from 1 to sqrt(2^31) 4. Quick calculation will show that of sqrt(2^31) = 46340.95. 5. After some calculation, you'll find out that there will be at most 4792 primes in the range 1 to 46340.95. So you only need about array of size (roughly) 4800 elements. 6. Generate that prime numbers from 1 to 46340.955. This will take time, but when you already have those 4792 primes in hand, you'll be able to use those values to determine whether a bigger number is a prime or not. 7. Now you have 4792 first primes in hand. All you have to do next is to check whether a big number N a prime or not by dividing it with small primes up to sqrt(N). If you can find at least one small primes can divide N, then N is not prime, otherwise N is prime. Fermat Little Test: This is a probabilistic algorithm so you cannot guarantee the possibility of getting correct answer. In a range as big as 1-1000000, Fermat Little Test can be fooled by (only) 255 Carmichael numbers (numbers that can fool Fermat Little Test, see Carmichael numbers above). However, you can do multiple random checks to increase this probability. Fermat Algorithm
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 08/10/2011.

Page1 / 20

Art_of_Programming_Contest_Part6 - CHAPTER 7 MATHEMATICS...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online