Saturday, June 20, 2015

[Programming book Chapter Ten] prime number.

A prime number (Prime Number) to mathematicians as a favorite, so much programaraderao favorite subject. At various times you will be able to solve problems of basic sankhyasankranta. Another way to understand it is important that the basic thing, the topic of this book, I've allocated a separate section. A prime number is an integer greater than the number who are 1 and divides it by the number 1 and will be just. A very simple thing. We have written a function of the parameters which determine the basic number is an integer number n. We function from n to n-1 up to 2 figures have tried to share with the help of a loop. If any of these numbers divides n, then we can say immediately that it is not a prime number and the function returns 0. And after all that you have seen any number divided by the number n is utterly unable to divide, then we can conclude that n is a prime number. The function of the return-to-1 Cor. We went to learn basic to evaluate! I felt that way when writing the program and it is a very simple way. Program at iphisiyenta (efficient) did not try to make. You can easily understand the matter. Give 2147483647 program as input. It takes quite a while to figure out that the prime number. 2147483647 to 2147483646 because the 2 is the number of failed attempts to divide. Program will iphisiyenta. Indeed, there has come an understanding of your head already. These are all the numbers from 2 to n-1 divided by 2 does not try to n / 2 is simply trying to share with the numbers. If you double the speed of the program will be. Another thing you may not notice. If the number is not divisible by 2 utterly, then divides it by the number of any other pair of the question. 2 apart from the other even numbers (4, 6, 8, ...), it is foolish to try to divide. Even with the number of divisibility test we can take at the beginning of the function. First, we have examined the value of n- is 2. 2 is given to the prime number n. Then we've tested do not even number n. If it is even, then n is prime number, only even prime number 2, which is the only test we have done at the outset. Then we have 3 n / 2 n is the number of all odds tried to share. Now you examine the program with different input. 2147483647 If you know that the speed of the program has increased more than ever before, but still feel a little time. My computer takes like four seconds. But I can not let so much time. The more you understand the math, they'll think of a way to speed up this program will be able to find out. It is for you to get the producers of n- n / 2 until the number of tests required. n- is simply the square root of the test. When n = pxq, p or q is equal to the square root of the number of the n- or smaller. We math.h header file to determine the root sqrt () function will use. Now the program will continue to examine the various inputs. Give a talk. There is a bug in programataya (ie wrong). Even if it is just to find out. Prime numbers to be able to find a lot of you happy? But we will not stop here and try. We now look for another way, the Greek mathematician iratosathenesa (Eratosthenes) today, two thousand years ago, this method was invented. The name of the sibha iratosathenesa (Sieve of Eratosthenes). Let's explain the procedure. Just in case, we'll get the number from 2 up to 40, all basic. Wrote all the numbers: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40. Now, watch, the first being the number 2 on the list. Now all multiples of 2 (2, with the exception that less than 2 baragulo araki) give out. If you have 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39. Now the second number 3 all multiples (more than 3 baragulo) Remove. 2, 3, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37. Now, the list of all the multiples of the number 5 (with the exception of 5) Leave it alone. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37. The next number is a multiple of 7, but it is futile to try to find. Because of the square root of the number 7 is less than 37. So 7 gunitakagulo list that was dropped from the list of those already. Understand the cause of the problem? Gunitakagulo on 7-of-14, 21, 28, 35. 7 the number of times that the gunitakagulo are available with all 7 numbers are smaller than the one we already have in their gunitakagulo. Now it's time to implement. We'll use an array to the list. Let's suppose that his name ara. In such a way to create the array, so that a number of n- status (ie, whether or not basic) ara [n] can be expressed with. If ara [n] - the value is 1, then the prime number n ara [n] - If the value is 0, n is not a prime number. Imaplimentesanera ago ayalagaridamata type: Step 1: Let's say there are elements of the array n. At the beginning of the array elements of the standard 1 tall. Step two: 1, whether the value of each component of the array tested. If 1, then go to the third step. Step 3: The number of 2 m with the serial numbers and product quality will be the array elements of the number zero (0) tall. It is not that the basic mark. Here m- m- with the values ​​of those numbers may be smaller than or equal to the product of n-. I've used a function to indicate the status of each array, print_ara (). Now we see that you input before the sieve () function call, have created the array. And every time I do not, there is no worry, but if the input is n ara [n] - was one of the standard test, the value of 1 if the prime number n, n is 0, then it is not a prime number. Do you want to count the number of how many will be required to sit in the size-. Now there is an issue with the speed of the program. Very fast (fast). But your eyes are no longer a problem? Do you understand that the cost of the program is much more memory? Just in case, we have 100 million so far to find out whether the basic number, then we will need an array of 100 million. "Time does not save memory, programmers often difficult to read. In case of problems, we can reach a solution between us. n- of the highest value to the value of its size- can take root. If you are called, n- maximum value of 100000000 (ten million) can be up to 10000 then the square root of the sieve to figure out the functions that primes. What will you do then? Well, I can not say anything, and you do not think that's right. We need to say another word. But replaced by an integer of four bytes, where one byte is a character. Instead of using an array of integers, so the characters are one-fourth of the cost of memory can bring it down. So we actually do not need an array of integers, the array will have only two values ​​0 or 1. In addition, there are several ways to further my writing sieve functions to increase efficiency. One of the qualities rather than being added. You try to do it.

No comments:

Post a Comment