Advertisement
Python253

palindromic_primes_finder_v2.08

Mar 13th, 2024
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: palindromic_primes_finder_v2.08.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. Palindromic Primes Finder:
  8.  
  9. This optimized version is approximately 60.47% faster than the version 2.07!
  10.  
  11. This Python script exclusively focuses on identifying prime numbers that are palindromes.
  12. It utilizes the equation [P = y^2 - y + 41], where 'y' is provided by the user.
  13. The script iterates through a sequence of numbers, checking for palindromic primes, and displays the results.
  14.  
  15. **Requirements:**
  16. - Python 3
  17.  
  18. **Usage:**
  19. 1. Run the script in a terminal or command prompt.
  20. 2. Enter the starting value for 'y'.
  21. 3. Specify the number of iterations to check.
  22. 4. The script will output prime numbers that are also palindromes along with their corresponding 'y' values.
  23.  
  24. **Note:**
  25. - The equation used is [P = y^2 - y + 41].
  26. - Only prime numbers that are palindromes will be displayed in the output.
  27.  
  28. Example:
  29. Enter the starting value for y: 41
  30. Enter the number of iterations: 10000000
  31.  
  32. Results:
  33. [y=131, p=17071]
  34. [y=1814, p=3288823]
  35. [y=2738, p=7493947]
  36. [y=2813, p=7910197]
  37. [y=12176, p=148242841]
  38. [y=13951, p=194616491]
  39. [y=17692, p=312989213]
  40. [y=281233, p=79091719097]
  41. [y=1372760, p=1884468644881]
  42. [y=1956282, p=3827037307283]
  43.  
  44. Processing time: 0 hours, 0 minutes, 7.526364 seconds
  45. """
  46.  
  47. import time
  48.  
  49. def is_prime(num):
  50.     """
  51.    Checks if a given number is prime.
  52.  
  53.    Args:
  54.        num (int): The number to check for primality.
  55.  
  56.    Returns:
  57.        bool: True if the number is prime, False otherwise.
  58.    """
  59.     if num < 2:
  60.         return False
  61.     if num == 2 or num == 3:
  62.         return True
  63.     if num % 2 == 0 or num % 3 == 0:
  64.         return False
  65.  
  66.     for i in range(5, int(num**0.5) + 1, 6):
  67.         if num % i == 0 or num % (i + 2) == 0:
  68.             return False
  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.  
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement