Prime Number judgment Algorithm

Source: Internet
Author: User

Reprinted: http://blog.csdn.net/liukehua123/article/details/5482854

Determine whether a number is a prime number. As we all know, if a number N is a combination, all its factors cannot exceed the square of SQRT (n) -- N, so we can use this property to use the most intuitive method.

To obtain all prime numbers less than or equal to n.

Num = 0;

For (I = 2; I <= N; I ++)

{For (j = 2; j <= SQRT (I); j ++)

If (J % I = 0) break;

If (j> SQRT (I) prime [num ++] = I; // This prime [] is of the int type, which is different from the following description.

}

This is the most common Algorithm for Solving prime numbers within n. The complexity is O (n * SQRT (N). If n is small, this algorithm (in fact, I doubt whether it is an algorithm and there is no level. Of course not.

Before getting started with the program competition, I would only use this method to calculate the prime number within n. -_-~) It does not take much time.

However, when n is large, for example, n = 10000000, N * SQRT (n)> 30000000000, which is an order of magnitude. Generally, it does not run within one second, but does not run for several minutes.

This is not my decision. If you want to exercise patience, try it ~....

In the program design competition, a better algorithm must be designed to identify all prime numbers within N in seconds or even seconds. So we have the prime screening method.

(Don't scold me if I'm not clear about it. I don't say a word when I see me ...)

The prime screening method is as follows:

1. Open a large bool array prime [], and the size is n + 1. First, mark all the subscripts with an odd number as true, and the subscript with an even number as false.

2. Then:

For (I = 3; I <= SQRT (n); I + = 2)

{If (prime [I])

For (j = I + I; j <= N; j + = I) prime [J] = false;

}

3. Finally, output the subscript of the Unit with the value of true in the bool array, which is the prime number within the desired n.

The principle is very simple, that is, when I is a prime number, all the multiples of I must be a sum. If I is determined not to be a prime number, find the prime number after I

Multiple of the number.

A simple process of screening prime numbers: n = 30.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 26 27 28 29 30

After step 2, step 2, 4... 28, 30, the 15 units are marked as false, and the remaining are true.

Start Step 1:

I = 3; because Prime [3] = true, put prime [6], [9], [12], [15], [18], [21], [24], [27], [30], marked as false.

I = 4; because Prime [4] = false, the screening process is not continued.

I = 5; because Prime [5] = true, prime [10], [15], [20], [25], and [30] are marked as false.

I = 6> SQRT (30) algorithm ends.

In step 2, output the subscript with the prime [] value true:

For (I = 2; I <= 30; I ++)

If (prime [I]) printf ("% d", I );

The result is 2 3 5 7 11 13 17 19 23 29.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.