Advertisement
Python253

is_prime

Mar 3rd, 2024 (edited)
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: is_prime.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. Is Prime Script
  8.  
  9. This script checks whether a user-input number is a prime number or a negative prime.
  10. Negative primes are prime numbers that are less than zero. They have the same properties as positive primes but with a negative sign. Examples include -2, -3, -5, 7... etc.
  11. It utilizes a function, is_prime, to perform the prime number check.
  12.  
  13. Requirements:
  14. - Python 3
  15.  
  16. Usage:
  17. 1. Run the script.
  18. 2. Enter a number when prompted.
  19. 3. The script will display whether the entered number is a prime number, a
  20.   negative prime, or not prime.
  21. """
  22.  
  23. def is_prime(number):
  24.     """Check if a number is a prime number.
  25.  
  26.    Args:
  27.        number (int): Number.
  28.  
  29.    Returns:
  30.        bool: True if number is a prime number, False otherwise.
  31.  
  32.    """
  33.     if number < 2:
  34.         return False
  35.     for x in range(2, int(abs(number)**0.5) + 1):
  36.         if abs(number) % x == 0:
  37.             return False
  38.     return True
  39.  
  40. def main():
  41.     # Get user input
  42.     user_input = int(input("Enter a number: "))
  43.  
  44.     # Check if it's a negative prime
  45.     if is_prime(abs(user_input)):
  46.         if user_input < 0:
  47.             print(f"The number {user_input} is a Negative Prime.")
  48.         else:
  49.             print(f"The number {user_input} is Prime.")
  50.     else:
  51.         print(f"The number {user_input} is Not Prime.")
  52.  
  53. if __name__ == "__main__":
  54.     main()
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement