Advertisement
Python253

palindromic_primes_finder_v2.07

Mar 12th, 2024 (edited)
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: palindromic_primes_finder_v2.07.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, 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.  
  26. Example:
  27. Enter the starting value for y: 41
  28. Enter the number of iterations: 1000000
  29.  
  30. Results:
  31. [y=131, p=17071]
  32. [y=1814, p=3288823]
  33. [y=2738, p=7493947]
  34. [y=2813, p=7910197]
  35. [y=12176, p=148242841]
  36. [y=13951, p=194616491]
  37. [y=17692, p=312989213]
  38. [y=281233, p=79091719097]
  39. [y=1372760, p=1884468644881]
  40. [y=1956282, p=3827037307283]
  41.  
  42. Processing time: 0 hours, 0 minutes, 19.046854 seconds
  43. """
  44.  
  45. import time
  46. from concurrent.futures import ThreadPoolExecutor
  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, batch_size=1000):
  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.        batch_size (int): The size of each batch for parallel processing.
  105.  
  106.    Returns:
  107.        None
  108.    """
  109.     results = []
  110.     start_time = time.time()
  111.  
  112.     with ThreadPoolExecutor() as executor:
  113.         args_list = range(start_y, start_y + iterations)
  114.  
  115.         for chunk_start in range(0, iterations, batch_size):
  116.             chunk_end = min(chunk_start + batch_size, iterations)
  117.             current_chunk = range(start_y + chunk_start, start_y + chunk_end)
  118.  
  119.             chunk_results = list(filter(None, executor.map(check_palindrome_prime, current_chunk)))
  120.             results.extend(chunk_results)
  121.  
  122.             # Clear memory
  123.             del chunk_results
  124.  
  125.     print("\nResults:")
  126.     if not results:
  127.         print("No Palindromic Primes found.")
  128.     else:
  129.         for result in results:
  130.             print(f"[y={result[0]}, p={result[1]}]")
  131.  
  132.     end_time = time.time()
  133.     elapsed_time = end_time - start_time
  134.  
  135.     hours, remainder = divmod(elapsed_time, 3600)
  136.     minutes, seconds = divmod(remainder, 60)
  137.     print(f"\nProcessing time: {int(hours)} hours, {int(minutes)} minutes, {seconds:.6f} seconds\n")
  138.  
  139. if __name__ == "__main__":
  140.     start_y = int(input("Enter the starting value for y: "))
  141.     iterations = int(input("Enter the number of iterations: "))
  142.     generate_primes_and_palindromes(start_y, iterations)
  143.  
  144.  
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement