Aks primes algorithm in python

When it comes to finding prime numbers in Python, there are several algorithms that can be used. In this article, we will explore three different approaches to solve the problem. Each solution will be presented with sample code and will be divided into different sections using

tags. Let’s get started!

Solution 1: Brute Force

The first solution is a brute force approach, where we iterate through all numbers up to the given input and check if each number is divisible by any number other than 1 and itself. If a number is not divisible by any other number, it is considered a prime number.


def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True

# Test the function
num = int(input("Enter a number: "))
if is_prime(num):
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")

Solution 2: Sieve of Eratosthenes

The second solution is a more efficient algorithm called the Sieve of Eratosthenes. This algorithm works by iteratively marking the multiples of each prime number, starting from 2, as composite (not prime). The remaining numbers after the iteration are prime numbers.


def sieve_of_eratosthenes(n):
    primes = [True] * (n + 1)
    primes[0] = primes[1] = False
    p = 2
    while p * p <= n:
        if primes[p]:
            for i in range(p * p, n + 1, p):
                primes[i] = False
        p += 1
    return [i for i in range(n + 1) if primes[i]]

# Test the function
num = int(input("Enter a number: "))
if num in sieve_of_eratosthenes(num):
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")

Solution 3: Trial Division with Optimization

The third solution is an optimized version of the brute force approach. Instead of iterating through all numbers up to the given input, we only need to check divisibility up to the square root of the input number. This optimization reduces the time complexity of the algorithm.


import math

def is_prime_optimized(n):
    if n <= 1:
        return False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

# Test the function
num = int(input("Enter a number: "))
if is_prime_optimized(num):
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")

After exploring these three solutions, it is clear that the optimized version of the brute force approach (Solution 3) is the best option. It provides a good balance between simplicity and efficiency, as it reduces the number of iterations required to determine if a number is prime. This optimization is particularly useful when dealing with large numbers.

Rate this post

4 Responses

  1. Wow, I never thought there were so many ways to implement the AKS Prime Algorithm in Python! Its fascinating to see the different solutions like Brute Force, Sieve of Eratosthenes, and Trial Division with Optimization. Which one do you think is the most efficient?

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents