Evaluate all prime numbers in the range of 1000,000 quickly. The complexity is O (n)

Source: Internet
Author: User

Today, we have gained a lot. We have created a fast O (n) Complexity to calculate all prime numbers in the range of 1-1000,000.

Note: This method is indeed self-developed. If you want to convert it, please enter this article. Otherwise, I will be very upset.

Idea: first define an array of isPrime [1000001] and initialize it to 0. isPrime [I] = 0 indicates that I is not sure whether it is a prime number, isPrime [I] = 1 indicates that I must not be a prime number.

For a number, it must be obtained by multiplying some prime numbers. If we obtain an x as a prime number, we can use x to remove the number that is greater than x.

However, this is not enough, because x traverses from 2 to 1000,000, and x multiples also need to traverse from x to 1000,000.

At this time, we can find that a connection can be optimized (when we talk about the word "determining" below, it means determining whether it is a prime number) (obviously, when x traverses an isPrime [x] = 0, it means that x is not regarded as a combination by a prime number smaller than x, and x is certainly not a factor greater than x, so x must be a prime number)

(1), x multiple x * y. When determining x * y, y does not have to be smaller than x, because if y is smaller than x, then x * y must have been determined before. How to explain: in two cases, if y is a prime number, x * y has been determined by y. If y is not a prime number, then x * y must have been determined by the y Factor. At this time, x * y does not need to be determined.

 

(2), x y, y must not have been determined as isPrime [y] = 1, that is, y must not be a prime number, that is, y is a Union number, at this time, the factor of y must be less than or equal to x (note that there is a case of equal, and the case explained later must be specially handled), because y has been determined, this factor must also be a factor of x * y, so x * y has been determined not to be a prime number.

 

For optimization, we can store all 2-1000,000 in a two-way linked list and use where [I] to represent the Node Address in the I linked list, when I is determined to be not a prime number, I can be deleted from the linked list. With where, I can be deleted without traversal.

Each time a prime number is obtained, the traversal table is used to obtain the number of keys that are determined to be a combination. After deleting the number of climblient linked list, the traversal jumps out until the number is greater than 1000,000. Then, each traversal of a node deletes a node, the complexity of this algorithm can be understood as the number of nodes that are traversed by a 1000,000-length linked list in O (n) operations. The total complexity is O (n ).

 

Note that we cannot delete a number z from the list if it is determined by the prime number x to the sum of numbers z. Why? Because all the factors of z are x, z is deleted, then z * x cannot be determined. In this case, we can delete z after z is used, and those cannot be reused, that is, if the value of z * x is greater than 1000,000, it is directly deleted from the linked list.

 

Note that int overflow exists, so we can define the value of the storage multiplication of an int64 variable.

 

Template code: the annotation part is the complexity of the algorithm I tested. For prime numbers less than 1000,000, only or steps are used.

# Include <stdio. h>
# Include <string. h>
# Deprecision MAX 1000000
Typedef _ int64 LL;

Struct List
{
List * last;
Int value;
List * next;
};

Bool isPrime [MAX + 1];

List * where [MAX + 1];

Int isResult [MAX + 1];

Int init ()
{
Int I;
LL temp, temp1;
List * p;
List * p1;
List * q1;
List * head;
// Int step = 0; // number of steps used for testing

Memset (isPrime, 0, sizeof (isPrime ));


For (I = 2; I <= MAX; I ++)
{
P = new List;
Where [I] = p;
If (I = 2)
{
P-> last = NULL;
P-> value = I;
P-> next = NULL;
Head = p;
}
Else
{
P-> last = where [I-1];
Where [I-1]-> next = p;

P-> value = I;
P-> next = NULL;
}
}

For (I = 2; I <= MAX; I ++)
{
If (isPrime [I])
Continue;
Else
{
For (p = head; p! = NULL; p = p-> next)
{
// Step ++; // number of steps used for testing
Temp = I * (LL) (p-> value );
If (isPrime [p-> value] & where [p-> value]! = NULL)
{
Temp1 = p-> value;
P1 = where [temp1]-> last;
Q1 = where [temp1]-> next;

P1-> next = q1;
If (q1! = NULL)
Q1-> last = p1;

Delete where [temp1];
Where [temp1] = NULL;
P = p1;
}
If (temp> MAX)
Break;
If (! IsPrime [temp])
{
IsPrime [temp] = 1;
If (temp * I> MAX)
{
P1 = where [temp]-> last;
Q1 = where [temp]-> next;
Delete where [temp];
Where [temp] = NULL;

P1-> next = q1;
If (q1! = NULL)
Q1-> last = p1;
}

}

}
}
}

// Printf ("% d \ n", step); // number of steps used for testing

}

 

 

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.