Advertisement
Python253

palindromic_primes_finder_v2.09

Mar 13th, 2024 (edited)
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: palindromic_primes_finder_v2.09.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'.
  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: 41
  29. Enter the number of iterations: 10000000
  30.  
  31. Results:
  32. [y=1814, p=3288823]
  33. [y=2738, p=7493947]
  34. [y=17692, p=312989213]
  35. [y=281233, p=79091719097]
  36. [y=1372760, p=1884468644881]
  37.  
  38. Processing time: 0 hours, 0 minutes, 5.640738 seconds
  39. """
  40.  
  41. # Imports
  42. import time
  43.  
  44. def is_prime(num):
  45.     """
  46.    Checks if a given number is prime.
  47.  
  48.    Args:
  49.        num (int): The number to check for primality.
  50.  
  51.    Returns:
  52.        bool: True if the number is prime, False otherwise.
  53.    """
  54.     if num < 2:
  55.         return False
  56.     if num == 2 or num == 3:
  57.         return True
  58.     if num % 2 == 0 or num % 3 == 0:
  59.         return False
  60.  
  61.     # Optimized primality check
  62.     i = 5
  63.     w = 2
  64.     while i * i <= num:
  65.         if num % i == 0:
  66.             return False
  67.         i += w
  68.         w = 6 - w
  69.  
  70.     return True
  71.  
  72. def is_palindrome(num):
  73.     """
  74.    Checks if a given number is a palindrome.
  75.  
  76.    Args:
  77.        num (int): The number to check for palindrome.
  78.  
  79.    Returns:
  80.        bool: True if the number is a palindrome, False otherwise.
  81.    """
  82.     str_num = str(num)
  83.     return str_num == str_num[::-1]
  84.  
  85. def check_palindrome_prime(y):
  86.     """
  87.    Checks if a given 'y' produces a palindromic prime.
  88.  
  89.    Args:
  90.        y (int): The 'y' value.
  91.  
  92.    Returns:
  93.        tuple: A tuple (y, P) if 'y' produces a palindromic prime, None otherwise.
  94.    """
  95.     P = y**2 - y + 41
  96.     return (y, P) if is_palindrome(P) else None
  97.  
  98. def generate_primes_and_palindromes(start_y, iterations):
  99.     """
  100.    Generates and identifies prime and palindromic prime numbers.
  101.  
  102.    Args:
  103.        start_y (int): The starting value for 'y'.
  104.        iterations (int): The number of iterations to check.
  105.  
  106.    Returns:
  107.        None
  108.    """
  109.     results = []
  110.     start_time = time.time()
  111.  
  112.     for y in range(start_y, start_y + iterations):
  113.         result = check_palindrome_prime(y)
  114.         if result:
  115.             results.append(result)
  116.  
  117.     print("\nResults:")
  118.     if not results:
  119.         print("No Palindromic Primes found.")
  120.     else:
  121.         for result in results:
  122.             print(f"[y={result[0]}, p={result[1]}]")
  123.  
  124.     end_time = time.time()
  125.     elapsed_time = end_time - start_time
  126.  
  127.     hours, remainder = divmod(elapsed_time, 3600)
  128.     minutes, seconds = divmod(remainder, 60)
  129.     print(f"\nProcessing time: {int(hours)} hours, {int(minutes)} minutes, {seconds:.6f} seconds\n")
  130.  
  131. if __name__ == "__main__":
  132.     start_y = int(input("Enter the starting value for y: "))
  133.     iterations = int(input("Enter the number of iterations: "))
  134.     generate_primes_and_palindromes(start_y, iterations)
  135.  
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement