Skip to content

Prime Numbers between 400 and 1000 #1691

Discussion options

You must be logged in to vote

Hey Check the code below

def is_prime(num):
    """Efficiently checks if a number is prime."""
    if num <= 1:
        return False
    if num <= 3:  # Optimized for 2 and 3
        return True
    if num % 2 == 0 or num % 3 == 0:  # Check divisibility by 2 and 3
        return False
    i = 5
    while i * i <= num:  # Check up to the square root
        if num % i == 0 or num % (i + 2) == 0:
            return False
        i += 6  # Optimized increment (6k ± 1)
    return True

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by vamsimessi10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants