Advertisement
Python253

primes_finder_plus

Mar 10th, 2024
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: primes_finder_plus.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. Primes Finder +Plus+:
  8.  
  9. This Python script identifies prime & palindromic prime numbers generated using the equation [P = y^2 - y + 41], where 'y' is a starting value provided by the user.
  10. The script iterates through a sequence of numbers, checks if each corresponds to a prime and subsequently palindromic primes, and displays the results to the terminal.
  11.  
  12. **Requirements:**
  13. - Python 3
  14.  
  15. **Usage:**
  16. 1. Run the script in a terminal or command prompt.
  17. 2. Enter the starting value for 'y'.
  18. 3. Specify the number of iterations to check.
  19. 4. The script will output primes & palindromic prime numbers along with their corresponding 'y' values.
  20.  
  21. **Note:**
  22. - The equation used is [P = y^2 - y + 41].
  23. - Prime numbers that are also palindromes will be highlighted in the output.
  24. """
  25.  
  26. def is_prime(num):
  27.     """
  28.    Checks if a given number is prime.
  29.  
  30.    Args:
  31.        num (int): The number to check for primality.
  32.  
  33.    Returns:
  34.        bool: True if the number is prime, False otherwise.
  35.    """
  36.     if num < 2:
  37.         return False
  38.     for i in range(2, int(num**0.5) + 1):
  39.         if num % i == 0:
  40.             return False
  41.     return True
  42.  
  43. def is_palindrome(num):
  44.     """
  45.    Checks if a given number is a palindrome.
  46.  
  47.    Args:
  48.        num (int): The number to check for palindrome.
  49.  
  50.    Returns:
  51.        bool: True if the number is a palindrome, False otherwise.
  52.    """
  53.     return str(num) == str(num)[::-1]
  54.  
  55. def generate_primes_and_palindromes(start_y, iterations):
  56.     """
  57.    Generates and identifies prime and palindromic prime numbers.
  58.  
  59.    Args:
  60.        start_y (int): The starting value for 'y'.
  61.        iterations (int): The number of iterations to check.
  62.  
  63.    Returns:
  64.        list: A list of formatted strings representing the results.
  65.    """
  66.     results = []
  67.     for y in range(start_y, start_y + iterations):
  68.         P = y**2 - y + 41
  69.  
  70.         if is_prime(P) and is_palindrome(P):
  71.             results.append(f"\033[31m[y={y}, pP={P}**]\033[0m")
  72.         elif is_prime(P):
  73.             results.append(f"[y={y}, P={P}*]")
  74.         elif is_palindrome(P):
  75.             results.append(f"\033[33m[y={y}, p={P}]\033[0m")
  76.  
  77.     return results
  78.  
  79. def main():
  80.     """
  81.    Main function to execute the script.
  82.    Takes user input for 'y' and iterations, then outputs the results to the terminal.
  83.    """
  84.     start_y = int(input("Enter the starting value for y: "))
  85.     iterations = int(input("Enter the number of iterations: "))
  86.     data = generate_primes_and_palindromes(start_y, iterations)
  87.  
  88.     print("\nResults:")
  89.     for result in data:
  90.         print(result)
  91.  
  92. if __name__ == "__main__":
  93.     main()
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement