![]() Because of this, we need only check 2 separately, then traverse odd numbers up to the square root of n. With some more efficient coding, we notice that you really only have to go up to the square root of n, because if you list out all of the factors of a number, the square root will always be in the middle (if it happens to not be an integer, we’re still ok, we just might over-approximate, but our code will still work).įinally, we know 2 is the “oddest” prime – it happens to be the only even prime number. This tells us we don’t have to try out all integers from 2 to n. ![]() Consider that if 2 divides some integer n, then (n/2) divides n as well. This doesn’t seem bad at first, but we can make it faster – much faster. Trivially, we can check every integer from 1 to itself (exclusive) and test whether it divides evenly.įor example, one might be tempted to run this algorithm: We learned numbers are prime if the only divisors they have are 1 and itself. In this tutorial, you will learn how to find whether a number is prime in simple cases. This is pretty useful when encrypting a password. ![]() ![]() A very important question in mathematics and security is telling whether a number is prime or not. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |