Advertisement
Python253

palindromic_primes_finder_v3.03

Mar 14th, 2024
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: palindromic_primes_finder_v3.03.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. Palindromic Primes Finder:
  8.  
  9. This Python script exclusively focuses on identifying prime numbers that are palindromes.
  10. It utilizes the equation [P = y^2 - y + 41], where 'y' is provided by the user.
  11. The script iterates through a sequence of numbers, checking for palindromic primes using a custom primality test, and displays the results.
  12.  
  13. **Requirements:**
  14. - Python 3
  15.  
  16. **Usage:**
  17. 1. Run the script in a terminal or command prompt.
  18. 2. Enter the starting value for 'y' (or press [ENTER] to exit).
  19. 3. Specify the number of iterations to check.
  20. 4. The script will output prime numbers that are also palindromes along with their corresponding 'y' values.
  21.  
  22. **Note:**
  23. - The equation used is [P = y^2 - y + 41].
  24. - Only prime numbers that are palindromes will be displayed in the output.
  25. - The script uses a custom primality test for checking prime numbers.
  26.  
  27. Example:
  28. Enter the starting value for y (or press [ENTER] to exit): 41
  29. Enter the number of iterations: 10000000
  30.  
  31. Results:
  32. [y=131, p=17071]
  33. [y=1814, p=3288823]
  34. [y=2738, p=7493947]
  35. [y=2813, p=7910197]
  36. [y=12176, p=148242841]
  37. [y=13951, p=194616491]
  38. [y=17692, p=312989213]
  39. [y=281233, p=79091719097]
  40. [y=1372760, p=1884468644881]
  41. [y=1956282, p=3827037307283]
  42.  
  43. Processing time: 0 hours, 0 minutes, 7.526364 seconds
  44. """
  45.  
  46. import time
  47.  
  48. def is_prime(num):
  49.     """
  50.    Checks if a given number is prime.
  51.  
  52.    Args:
  53.        num (int): The number to check for primality.
  54.  
  55.    Returns:
  56.        bool: True if the number is prime, False otherwise.
  57.    """
  58.     if num < 2:
  59.         return False
  60.     if num == 2 or num == 3:
  61.         return True
  62.     if num % 2 == 0 or num % 3 == 0:
  63.         return False
  64.  
  65.     for i in range(5, int(num**0.5) + 1, 6):
  66.         if num % i == 0 or num % (i + 2) == 0:
  67.             return False
  68.  
  69.     return True
  70.  
  71. def is_palindrome(num):
  72.     """
  73.    Checks if a given number is a palindrome.
  74.  
  75.    Args:
  76.        num (int): The number to check for palindrome.
  77.  
  78.    Returns:
  79.        bool: True if the number is a palindrome, False otherwise.
  80.    """
  81.     str_num = str(num)
  82.     return str_num == str_num[::-1]
  83.  
  84. def check_palindrome_prime(y):
  85.     """
  86.    Checks if a given 'y' produces a palindromic prime.
  87.  
  88.    Args:
  89.        y (int): The 'y' value.
  90.  
  91.    Returns:
  92.        tuple: A tuple (y, P) if 'y' produces a palindromic prime, None otherwise.
  93.    """
  94.     P = y**2 - y + 41
  95.     return (y, P) if is_palindrome(P) else None
  96.  
  97. def generate_primes_and_palindromes(start_y, iterations):
  98.     """
  99.    Generates and identifies prime and palindromic prime numbers.
  100.  
  101.    Args:
  102.        start_y (int): The starting value for 'y'.
  103.        iterations (int): The number of iterations to check.
  104.  
  105.    Returns:
  106.        None
  107.    """
  108.     results = []
  109.     start_time = time.time()
  110.  
  111.     for y in range(start_y, start_y + iterations):
  112.         result = check_palindrome_prime(y)
  113.         if result:
  114.             results.append(result)
  115.  
  116.     print("\nResults:")
  117.     if not results:
  118.         print("No Palindromic Primes found.")
  119.     else:
  120.         for result in results:
  121.             print(f"[y={result[0]}, p={result[1]}]")
  122.  
  123.     end_time = time.time()
  124.     elapsed_time = end_time - start_time
  125.  
  126.     hours, remainder = divmod(elapsed_time, 3600)
  127.     minutes, seconds = divmod(remainder, 60)
  128.     print(f"\nProcessing time: {int(hours)} hours, {int(minutes)} minutes, {seconds:.6f} seconds\n")
  129.  
  130. if __name__ == "__main__":
  131.     while True:
  132.         start_y_input = input("Enter the starting value for y (or press [ENTER] to exit): ")
  133.         if start_y_input == "":
  134.             print("Exiting...")
  135.             break
  136.         try:
  137.             start_y = int(start_y_input)
  138.             iterations = int(input("Enter the number of iterations: "))
  139.             generate_primes_and_palindromes(start_y, iterations)
  140.         except ValueError:
  141.             print("Invalid input. Please try again.")
  142.  
  143.  
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement