Advertisement
Python253

palindromic_primes_finder_optimized_v2.04

Mar 11th, 2024 (edited)
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: palindromic_primes_finder_optimized_v2.04.py
  4. # Version: 2.04
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Palindromic Primes Finder:
  9.  
  10. This Python script focuses exclusively on identifying prime numbers that are palindromes, utilizing 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. Finally giving the user the processing time elapsed in the terminal.
  13.  
  14. **Requirements:**
  15. - Python 3
  16.  
  17. **Usage:**
  18. 1. Run the script in a terminal or command prompt.
  19. 2. Enter the starting value for 'y'.
  20. 3. Specify the number of iterations to check.
  21. 4. The script will output prime numbers that are also palindromes along with their corresponding 'y' values.
  22. 5. Outputs the elapsed time to process in the terminal.
  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=17692, p=312989213]
  37. [y=281233, p=79091719097]
  38. [y=1372760, p=1884468644881]
  39.  
  40. Processing time: 164.902216 seconds
  41. Processing time: 0 hours, 30 minutes, 18.918295 seconds
  42. """
  43.  
  44. import random
  45. import time
  46.  
  47. def is_prime(num, k=5):
  48.     if num < 2:
  49.         return False
  50.     if num == 2 or num == 3:
  51.         return True
  52.     if num % 2 == 0 or num % 3 == 0:
  53.         return False
  54.  
  55.     # Use a deterministic Miller-Rabin for smaller numbers
  56.     if num < 1373653:
  57.         return all(num % p != 0 for p in [2, 3, 5, 7, 11, 13, 17])
  58.  
  59.     s, d = 0, num - 1
  60.     while d % 2 == 0:
  61.         s += 1
  62.         d //= 2
  63.  
  64.     for _ in range(k):
  65.         a = random.randint(2, min(num - 2, 10**5))
  66.         x = pow(a, d, num)
  67.         if x == 1 or x == num - 1:
  68.             continue
  69.  
  70.         for _ in range(s - 1):
  71.             x = pow(x, 2, num)
  72.             if x == num - 1:
  73.                 break
  74.         else:
  75.             return False
  76.  
  77.     return True
  78.  
  79. def is_palindrome(num):
  80.     str_num = str(num)
  81.     return str_num == str_num[::-1]
  82.  
  83. def generate_primes_and_palindromes(start_y, iterations):
  84.     results = []
  85.     start_time = time.time()
  86.  
  87.     for y in range(start_y, start_y + iterations):
  88.         P = y**2 - y + 41
  89.  
  90.         if is_prime(P) and is_palindrome(P):
  91.             results.append(f"[y={y}, p={P}]")
  92.  
  93.     end_time = time.time()
  94.     elapsed_time = end_time - start_time
  95.  
  96.     print("\nResults:")
  97.     if not results:
  98.         print("No Palindromic Primes found.")
  99.     else:
  100.         for result in results:
  101.             print(result)
  102.  
  103.     hours, remainder = divmod(elapsed_time, 3600)
  104.     minutes, seconds = divmod(remainder, 60)
  105.     print(f"\nProcessing time: {int(hours)} hours, {int(minutes)} minutes, {seconds:.6f} seconds\n")
  106.  
  107.  
  108. if __name__ == "__main__":
  109.     start_y = int(input("Enter the starting value for y: "))
  110.     iterations = int(input("Enter the number of iterations: "))
  111.     generate_primes_and_palindromes(start_y, iterations)
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement