Advertisement
Python253

palindromic_primes_finder

Mar 10th, 2024 (edited)
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 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. Example:
  26. Enter the starting value for y: 41
  27. Enter the number of iterations: 10
  28.  
  29. Results:
  30. [y=43, P=1847*]
  31. [y=44, P=1933*]
  32. [y=46, P=2111*]
  33. [y=47, P=2203*]
  34. [y=48, P=2297*]
  35. [y=49, P=2393*]
  36. """
  37.  
  38. def is_prime(num):
  39.     """
  40.    Checks if a given number is prime.
  41.  
  42.    Args:
  43.        num (int): The number to check for primality.
  44.  
  45.    Returns:
  46.        bool: True if the number is prime, False otherwise.
  47.    """
  48.     if num < 2:
  49.         return False
  50.     for i in range(2, int(num**0.5) + 1):
  51.         if num % i == 0:
  52.             return False
  53.     return True
  54.  
  55. def is_palindrome(num):
  56.     """
  57.    Checks if a given number is a palindrome.
  58.  
  59.    Args:
  60.        num (int): The number to check for palindrome.
  61.  
  62.    Returns:
  63.        bool: True if the number is a palindrome, False otherwise.
  64.    """
  65.     return str(num) == str(num)[::-1]
  66.  
  67. def generate_primes_and_palindromes(start_y, iterations):
  68.     """
  69.    Generates and identifies prime and palindromic prime numbers.
  70.  
  71.    Args:
  72.        start_y (int): The starting value for 'y'.
  73.        iterations (int): The number of iterations to check.
  74.  
  75.    Returns:
  76.        list: A list of formatted strings representing the results.
  77.    """
  78.     results = []
  79.     for y in range(start_y, start_y + iterations):
  80.         P = y**2 - y + 41
  81.  
  82.         if is_prime(P) and is_palindrome(P):
  83.             results.append(f"\033[31m[y={y}, pP={P}**]\033[0m")
  84.         elif is_prime(P):
  85.             results.append(f"[y={y}, P={P}*]")
  86.         elif is_palindrome(P):
  87.             results.append(f"\033[33m[y={y}, p={P}]\033[0m")
  88.  
  89.     return results
  90.  
  91. def main():
  92.     """
  93.    Main function to execute the script.
  94.    Takes user input for 'y' and iterations, then outputs the results to the terminal.
  95.    """
  96.     start_y = int(input("Enter the starting value for y: "))
  97.     iterations = int(input("Enter the number of iterations: "))
  98.     data = generate_primes_and_palindromes(start_y, iterations)
  99.  
  100.     print("\nResults:")
  101.     for result in data:
  102.         print(result)
  103.  
  104. if __name__ == "__main__":
  105.     main()
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement