In this tutorial, you learned how to use Python to check if a number is a prime number. Let’s see how we can do this for the values from 100 through 300: # Finding All Prime Numbers Between 100 and 300 ![]() In order to do this, we can use our optimized function above and loop over a range of numbers to return all values that are prime numbers. We can see that the square root method is significantly faster than the other methods! Finding all Prime Numbers in a Range of NumbersĪ common challenge will be to find all the prime numbers between two different numbers. The performance of our prime number functions The table below breaks down the performance between these three functions: Function Since we know this number is a prime number, the function will need to run through all iterations. In order to test these, let’s use a large prime number, 433,494,437. Now that we’ve developed three functions, we can easily compare the performance of these functions to see the performance gains that we’ll get from them. Comparing Performance of Prime Number Functions In the next section, you’ll learn how to find all the prime numbers in a range of numbers. Let’s see how this looks: # The Final Function to Check for Prime Numbersįor num in range(2, int(number**0.5) + 1): This can have significant improvement on the number of checks the function needs to make. We can actually take the square root of the number that we’re checking. Now let’s take a look at one more improvement we can make. We use floored division to divide by two and return an integer.In the function above, we’e made the following improvement: This means that the number of iterations that our code has to complete is reduced by roughly half! Let’s see how we can write this function: # Improving our function We can divide the number by 2 since any composite number (a non-prime number) will also have a factor of its half. This function works well! However, it’s not the most efficient function. If the modulo of the number and the iteration is equal to zero (meaning the number is divided cleanly), then the function returns False.It then loops over the range from 2 through to the number (but not including).If the number is not greater than 1, then the function returns False as prime numbers need to be larger than 1.We defined a function is_prime that takes a single argument, a number.Let’s take a look at how we can implement this first code: # Writing a Simple Function to Check if a Number is a Prime Number If that occurs, then the number has a divisor other than 1 and the number itself and the number isn’t a prime number. The most naive and straightforward implementation is to loop over the range of numbers from 2 to the number and see if the modulo of the number and the range is equal to 0. Let’s take a look at how we can use Python to determine if a number is a prime number. Finding Prime Numbers in Python (Optimized Code) The first few prime numbers are: 3, 7, 11, 13, etc. For example, the number 5 is a prime number, while the number 6 isn’t (since 2 x 3 is equal to 6). Prime numbers are a positive integer that’s greater than 1 that also have no other factors except for 1 and the number itself. Finding all Prime Numbers in a Range of Numbers.Comparing Performance of Prime Number Functions.Finding Prime Numbers in Python (Optimized Code).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |