Find primes | Li Ming card six state a
So, if the first two attempts, then for all the odd-numbered 3 to x the trial division, is not gifted enough to pinch? Well the answer is clearly no? I write to you, oh just beginning to warm up. Some of the more clever ape program, you will find a problem: from 3 to try all odd x, or some waste. For example, to determine whether or not a prime number 101, after rounding root of 101 is 10, so, according to the state 4, odd to try are: 3,5,7,9. But you do not find on 9 attempts are superfluous. Can not be divisible by 3, must not be divisible by 9 .. ... Along the way to go, you will find these programs ape: Actually, just try to be less than x primes. These primes, used shipping containers just as already counted out (is not that a very wonderful?). So, in this realm of program ape, will have calculated the number of quality, save up, and then for the subsequent test in addition, efficiency is greatly improved. Incidentally, this is the theory of algorithms often mentioned: space for time.
Four kinds of state at the beginning, followed by progressive basically. However, the state with the state 4 5, is flat stages. I examine the candidates had, it was thought, but did not think the realm realm 4 5; on the contrary, it was thought used shipping containers realm realm 4 5 but did not expect. Typically, these two realms as long as one can think of, I will give 5-7 points; if the two are thought of, I will give 8-10 points. For the "junior software engineer" I want to move the post, it could also think of the realm of 4 and 5 state, should be on it. If you do not ask yourself, just be satisfied tasted. Well, see here, you'll be able to come to a halt, and no need to look at the contents of the follow-up; on the contrary, if you are curious or want to learn a lot more, please used shipping containers read on.
Having "test division," Come talk sieve method (Wikipedia used shipping containers explanation "here"). I wish to speculate about: the readers of this article, there should be more than 2/3, never heard of sifting method. So pinch, by the way twitched eggs with everybody, and talk about the origin of the sieve method. This screening method ah, really is both clever and fast primes seeking methods. Its inventor used shipping containers was around 250 BC, a Greek cattle - Eratosthenes. Why Daniel said he was pinch? Because used shipping containers he himself proficient in a number of disciplines and fields, at least include: mathematics, astronomy, geography (geography used shipping containers of this term, that he founded), history, literature (he is a poet). Really be called "cross-cutting Daniel." I admire him most is: just a simple geometric method to measure the distance of the circumference of the Earth, the Earth and the moon, the distance between Earth and the sun's equator and the ecliptic angle ...... Moreover, these calculations with contemporary scientists measured, almost the same. To know the age of his life, roughly equivalent to China's Spring and Autumn. And our ancestors, until the Ming Dynasty still stubbornly believe: the sky is round and the earth is square, the moon will be Nintendogs used shipping containers to eat myself ...... well, Chedan finished, get down to business. Many people estimate sieve method only as a concrete way. In fact, the sieve method is a very universal idea. When dealing with a lot of complex used shipping containers issues, you can see the shadow of sieve method. So, how to find a prime number sieve method pinch, that it is very simple: First, the two smallest prime number used shipping containers is recognized, therefore, first remove all multiples of 2; then the rest of the number of those which is greater than 2, the minimum used shipping containers is 3, so 3 is also a prime number; then all multiples of 3 are removed, used shipping containers the number of those remaining inside greater than 3, the minimum is 5, so 5 is a prime number ...... the above process is repeated, you can put a few together a range of all remove (like being screened out like a sieve), the rest is a prime number. Have a very vivid animation on Wikipedia, can directly reflect the work process sieve method.
But demand for demand 2 2 watches used shipping containers is. Because N needs two given indicates the number of prime numbers to be printed, then the N primes will be distributed to what extent pinch? This is a headache ah. However, to apply for the program if cattle ape enough, of course, this problem will not be stumped. Because the distribution of prime numbers, there is a pattern drops - this is the famous prime number theorem. A little math, you should know the distribution of prime numbers is more backward the more sparse. Or that the density of primes is getting low. The prime number theorem, that the white mathematicians found some formulas used to estimate a range of prime numbers, there are about a few. In these formulas, is the most simple x / ln (x), the formula ln denotes the natural used shipping containers logarithm (estimated that many students have forgotten Shajiao natural logarithm). Suppose you want to estimate the number of primes less than 1,000,000, calculated using the formula is a 72,382, while there are 78,498 an actual error of about 8 percentage points. Features of the formula is: the larger the estimated range, the smaller the error rate. With the prime number theorem, we can count the number you want to print quality, anti-introduction of these primes distributed in much range. Because of this there is a certain prime number distribution formula errors (typically less than 15%). For insurance purposes, used shipping containers the anti-launched distribution of prime numbers and then slightly expanded 15%, should be sufficient.
Let us talk about the most usual realm of a soil to engage in law - Direct construct an integer container. In the screening used shipping containers process of the number of co-discovered deleted, the last container on the left of the primes. Why do say that this method is the most earth pinch? First, integer container, waste of memory space. Let's say you are using a 32-bit C / C ++ or Java, then each int have spent at least 4 bytes of memory. When N is large, the memory overhead on a problem. Secondly, when N is large, frequently on a large container delete operation may cause frequent memory allocation and release (depending on implementation of the container); and frequent memory allocation / deallocation, will result in significant CPU occupancy and may cause memory fragmentation.
2 In order to avoid a realm realm disadvantages caused apes smarter program will construct a fixed-length boolean container (usually an array). For example, distribution of prime numbers is 1,000,000, then construct an array of boolean contains 1,000,000. Then put all the elements are initialized to true. In the screening process, once found a natural used shipping containers number is a composite used shipping containers number, with regard to the natural numbers to the next standard, the corresponding boolean value to false. After all screening finished, through the array, find true value of those elements used shipping containers and put them under the label can be printed out. The advantage of this realm are: First, since the container is fixed length, computation process used shipping containers to avoid the frequent memory allocation / deallocation; Second, in some languages, Boolean footprint smaller than an integer. For example, the C ++ bool only 1 byte Note: C ++ standard (ISO / IEC 14882) is not mandatory sizeof (bool) == 1, but most compilers are implemented as a byte.
3 Although the two state solution used shipping containers to the realm of the drawbacks of a state, but there are still a lot of room for optimization. Some programs will come up ape bit (bit) storage ideas. It is in fact on the basis of the realm of 2 to optimize the performance space. I think: C / C ++ or origin played assembly language, it is easier want this talent. In C ++, for example. A bool occupies one byte of memory. While a byte has eight bits, each bit may represent 0 or 1. So, when you use the stored bit mode, a byte can be used when the eight Boolean use. Therefore, to achieve this state of the program ape, will construct a fixed length of each byte is stored byte array, the array of eight Boolean value. Performance compared to the realm of space 2, increased by 8 times (for C ++ terms). If a language using Boolean 4 bytes, then state 3 to state 2, the space utilization increased 32-fold.
See I write "summary" used shipping containers word, a lot of friends I thought: finally read, and know how to find a prime number is optimal. Actually, you are wrong, this article used shipping containers was written less than half. Considering the space has been a bit long, but I hit so many words, but also a little tired, temporarily stopped talking, then the next chat. I hope today's presentation looked, and everybody used shipping containers should understand the principle: Mountains Beyond used shipping containers Mountains, is day. Every small branch inside every technical field, go deeper, there are a lot of doorways and secret. You go into the process, certainly learned a lot. You get to the bottom of the course is the ability to improve the process. Subsequent article content, will introduce mentioned bit storage method used shipping containers What defects, how to solve. In addition, other methods will also seek to introduce some primes.
<> Article 27 states: "Macao residents shall have freedom of speech Press, publisher of association parade, rights of assembly and freedom of demonstration."
So, if the first two attempts, then for all the odd-numbered 3 to x the trial division, is not gifted enough to pinch? Well the answer is clearly no? I write to you, oh just beginning to warm up. Some of the more clever ape program, you will find a problem: from 3 to try all odd x, or some waste. For example, to determine whether or not a prime number 101, after rounding root of 101 is 10, so, according to the state 4, odd to try are: 3,5,7,9. But you do not find on 9 attempts are superfluous. Can not be divisible by 3, must not be divisible by 9 .. ... Along the way to go, you will find these programs ape: Actually, just try to be less than x primes. These primes, used shipping containers just as already counted out (is not that a very wonderful?). So, in this realm of program ape, will have calculated the number of quality, save up, and then for the subsequent test in addition, efficiency is greatly improved. Incidentally, this is the theory of algorithms often mentioned: space for time.
Four kinds of state at the beginning, followed by progressive basically. However, the state with the state 4 5, is flat stages. I examine the candidates had, it was thought, but did not think the realm realm 4 5; on the contrary, it was thought used shipping containers realm realm 4 5 but did not expect. Typically, these two realms as long as one can think of, I will give 5-7 points; if the two are thought of, I will give 8-10 points. For the "junior software engineer" I want to move the post, it could also think of the realm of 4 and 5 state, should be on it. If you do not ask yourself, just be satisfied tasted. Well, see here, you'll be able to come to a halt, and no need to look at the contents of the follow-up; on the contrary, if you are curious or want to learn a lot more, please used shipping containers read on.
Having "test division," Come talk sieve method (Wikipedia used shipping containers explanation "here"). I wish to speculate about: the readers of this article, there should be more than 2/3, never heard of sifting method. So pinch, by the way twitched eggs with everybody, and talk about the origin of the sieve method. This screening method ah, really is both clever and fast primes seeking methods. Its inventor used shipping containers was around 250 BC, a Greek cattle - Eratosthenes. Why Daniel said he was pinch? Because used shipping containers he himself proficient in a number of disciplines and fields, at least include: mathematics, astronomy, geography (geography used shipping containers of this term, that he founded), history, literature (he is a poet). Really be called "cross-cutting Daniel." I admire him most is: just a simple geometric method to measure the distance of the circumference of the Earth, the Earth and the moon, the distance between Earth and the sun's equator and the ecliptic angle ...... Moreover, these calculations with contemporary scientists measured, almost the same. To know the age of his life, roughly equivalent to China's Spring and Autumn. And our ancestors, until the Ming Dynasty still stubbornly believe: the sky is round and the earth is square, the moon will be Nintendogs used shipping containers to eat myself ...... well, Chedan finished, get down to business. Many people estimate sieve method only as a concrete way. In fact, the sieve method is a very universal idea. When dealing with a lot of complex used shipping containers issues, you can see the shadow of sieve method. So, how to find a prime number sieve method pinch, that it is very simple: First, the two smallest prime number used shipping containers is recognized, therefore, first remove all multiples of 2; then the rest of the number of those which is greater than 2, the minimum used shipping containers is 3, so 3 is also a prime number; then all multiples of 3 are removed, used shipping containers the number of those remaining inside greater than 3, the minimum is 5, so 5 is a prime number ...... the above process is repeated, you can put a few together a range of all remove (like being screened out like a sieve), the rest is a prime number. Have a very vivid animation on Wikipedia, can directly reflect the work process sieve method.
But demand for demand 2 2 watches used shipping containers is. Because N needs two given indicates the number of prime numbers to be printed, then the N primes will be distributed to what extent pinch? This is a headache ah. However, to apply for the program if cattle ape enough, of course, this problem will not be stumped. Because the distribution of prime numbers, there is a pattern drops - this is the famous prime number theorem. A little math, you should know the distribution of prime numbers is more backward the more sparse. Or that the density of primes is getting low. The prime number theorem, that the white mathematicians found some formulas used to estimate a range of prime numbers, there are about a few. In these formulas, is the most simple x / ln (x), the formula ln denotes the natural used shipping containers logarithm (estimated that many students have forgotten Shajiao natural logarithm). Suppose you want to estimate the number of primes less than 1,000,000, calculated using the formula is a 72,382, while there are 78,498 an actual error of about 8 percentage points. Features of the formula is: the larger the estimated range, the smaller the error rate. With the prime number theorem, we can count the number you want to print quality, anti-introduction of these primes distributed in much range. Because of this there is a certain prime number distribution formula errors (typically less than 15%). For insurance purposes, used shipping containers the anti-launched distribution of prime numbers and then slightly expanded 15%, should be sufficient.
Let us talk about the most usual realm of a soil to engage in law - Direct construct an integer container. In the screening used shipping containers process of the number of co-discovered deleted, the last container on the left of the primes. Why do say that this method is the most earth pinch? First, integer container, waste of memory space. Let's say you are using a 32-bit C / C ++ or Java, then each int have spent at least 4 bytes of memory. When N is large, the memory overhead on a problem. Secondly, when N is large, frequently on a large container delete operation may cause frequent memory allocation and release (depending on implementation of the container); and frequent memory allocation / deallocation, will result in significant CPU occupancy and may cause memory fragmentation.
2 In order to avoid a realm realm disadvantages caused apes smarter program will construct a fixed-length boolean container (usually an array). For example, distribution of prime numbers is 1,000,000, then construct an array of boolean contains 1,000,000. Then put all the elements are initialized to true. In the screening process, once found a natural used shipping containers number is a composite used shipping containers number, with regard to the natural numbers to the next standard, the corresponding boolean value to false. After all screening finished, through the array, find true value of those elements used shipping containers and put them under the label can be printed out. The advantage of this realm are: First, since the container is fixed length, computation process used shipping containers to avoid the frequent memory allocation / deallocation; Second, in some languages, Boolean footprint smaller than an integer. For example, the C ++ bool only 1 byte Note: C ++ standard (ISO / IEC 14882) is not mandatory sizeof (bool) == 1, but most compilers are implemented as a byte.
3 Although the two state solution used shipping containers to the realm of the drawbacks of a state, but there are still a lot of room for optimization. Some programs will come up ape bit (bit) storage ideas. It is in fact on the basis of the realm of 2 to optimize the performance space. I think: C / C ++ or origin played assembly language, it is easier want this talent. In C ++, for example. A bool occupies one byte of memory. While a byte has eight bits, each bit may represent 0 or 1. So, when you use the stored bit mode, a byte can be used when the eight Boolean use. Therefore, to achieve this state of the program ape, will construct a fixed length of each byte is stored byte array, the array of eight Boolean value. Performance compared to the realm of space 2, increased by 8 times (for C ++ terms). If a language using Boolean 4 bytes, then state 3 to state 2, the space utilization increased 32-fold.
See I write "summary" used shipping containers word, a lot of friends I thought: finally read, and know how to find a prime number is optimal. Actually, you are wrong, this article used shipping containers was written less than half. Considering the space has been a bit long, but I hit so many words, but also a little tired, temporarily stopped talking, then the next chat. I hope today's presentation looked, and everybody used shipping containers should understand the principle: Mountains Beyond used shipping containers Mountains, is day. Every small branch inside every technical field, go deeper, there are a lot of doorways and secret. You go into the process, certainly learned a lot. You get to the bottom of the course is the ability to improve the process. Subsequent article content, will introduce mentioned bit storage method used shipping containers What defects, how to solve. In addition, other methods will also seek to introduce some primes.
<> Article 27 states: "Macao residents shall have freedom of speech Press, publisher of association parade, rights of assembly and freedom of demonstration."
No comments:
Post a Comment