Python

Identify Prime Numbers using Python

Identify Prime Numbers using Python

Introduction

You will learn how to identify prime numbers using Python in this lesson, either by determining whether a certain value is a prime number or by locating all prime numbers within a specified range of values. Prime numbers are those that only have the number itself and the number 1 as components. Working with prime numbers is a skill that is frequently used in fields like computer and cyber security.

After finishing this tutorial, you will know:

  • What are the prime numbers?
  • Several Python methods for determining prime numbers Python method optimization for discovering prime numbers.
  • Using Python, find all prime integers inside a specified range of values

What are Prime Numbers

Positive integers larger than 1 that are prime numbers have no additional factors than 1 and the number itself. In contrast to the number 6, the number 5 is a prime number (since 2 x 3 is equal to 6). 3, 7, 11, 13, and other prime numbers are among the first few.

Finding Prime Numbers in Python (Optimized Code)

Let's see how Python may be used to discover whether a given number is a prime number. The simplest and most basic approach is to iterate through the range of integers from 2 to the number, checking if the modulo of the number and the range equals 0. If that happens, the number isn't a prime number since it contains a divisor other than 1 in addition to the number itself.

Let's see how we may put this first piece of code into practice:

def is_prime(number):
    if number > 1:
        for num in range(2, number):
            if number % num == 0:
                return False
        return True
    return False

print(is_prime(4))

# Returns: False

write your code here: Coding Playground

Let's examine what we accomplished in more detail: We created a function called prime that only accepts an integer as an input.

  1. Since prime numbers must be bigger than 1, the method returns False if the value is not more than 1.
  2. Then it repeats the range from 2 to the number (but not including).
  3. The function returns False if the integer is split neatly by the iteration's modulo, which should be equal to zero.
  4. Should it happen, the method returns True.

This feature is effective. It isn't the most effective function, though. Since every composite number (a non-prime number) will also have a component of its half, we may divide the number by 2. This implies an approximately 50% reduction in the number of iterations required by our code! Let's examine the best way to create this function:

def is_prime(number):
    if number > 1:
        for num in range(2, number // 2 + 1):
            if number % num == 0:
                return False
        return True
    return False

print(is_prime(941))

write your code here: Coding Playground

The following enhancement was made to the function above: Divide by two using floored division, and output the result as an integer. In order to raise that number, we add one. Let's now consider one additional enhancement we can make. Actually, we can calculate the square root of the number we're examining. This may significantly reduce the amount of checks the function has to do. Check out how this appears:

def is_prime(number):
    if number > 1:
        for num in range(2, int(number**0.5) + 1):
            if number % num == 0:
                return False
        return True
    return False

print(is_prime(2011))

# Returns: True

write your code here: Coding Playground

All Primes in a Number Range

Finding all the primes between two distinct numbers is a popular issue. This may be accomplished by looping through a set of integers to return all integers that are prime numbers using the above-mentioned optimized algorithm. Let's see how we might accomplish this for the range of numbers from 100 to 300:

# Finding All Prime Numbers Between 200 and 300
prime_numbers = []
for num in range(200, 301):
    if is_prime(num):
        prime_numbers.append(num)

print(prime_numbers)

# Returns:
# [211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]

write your code here: Coding Playground

Conclusion

You discovered how to use Python to determine whether a number is a prime number in this tutorial. You initially learned a crude implementation, then discovered how to greatly cut the runtime of your function by optimizing it. Finally, you discovered how to use this improved function to locate all prime numbers inside a certain range of integers.