DEV Community

Cover image for Prime Number Tester
Scott Gordon
Scott Gordon

Posted on

1

Prime Number Tester

# prime_tester.py
#   This program accepts a n value as input and determines whether
#   or not it is prime.
# by: Scott Gordon

from math import sqrt


def is_prime(n):
    """Determines whether number (n) is prime or not"""
    if n % 2 == 0 and n != 2:
        return False
    factor = 3
    while factor <= sqrt(n):
        if n % factor == 0:
            return False
        factor += 2
    return True


def main():
    print("Prime Number Tester\n")
    n = int(input("Enter a number: "))
    if is_prime(n):
        print(f"{n} is prime.")
    else:
        print(f"{n} Is not prime.")


if __name__ == '__main__':
    main()

Enter fullscreen mode Exit fullscreen mode

Photo by Ryan Johns on Unsplash

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay