Advertisement
Python253

pp_in_range_11_to_1t

Mar 14th, 2024
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: pp_in_range_11_to_1t.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. Find and save Prime Palindromes within the range from 11 to 1 trillion.
  8.  
  9. Requirements:
  10. - Python 3.x
  11.  
  12. Usage:
  13. 1. Ensure you have Python 3.x installed on your system.
  14. 2. Run the script.
  15.  
  16. Important Notes:
  17. - The script finds Prime Palindromes within the range from 11 to 1 trillion.
  18. - It skips values less than 11 since the first palindromic prime is 11.
  19. - Prime Palindromes found within each million-range are saved to separate text files.
  20. - The script may take some time to execute, especially for larger ranges.
  21. - Make sure you have sufficient disk space to save the output files.
  22. """
  23.  
  24. def is_prime(n):
  25.     """
  26.    Check if a number is prime.
  27.  
  28.    Parameters:
  29.    - n (int): The number to check.
  30.  
  31.    Returns:
  32.    - bool: True if the number is prime, False otherwise.
  33.    """
  34.     if n <= 1:
  35.         return False
  36.     if n <= 3:
  37.         return True
  38.     if n % 2 == 0 or n % 3 == 0:
  39.         return False
  40.     i = 5
  41.     while i * i <= n:
  42.         if n % i == 0 or n % (i + 2) == 0:
  43.             return False
  44.         i += 6
  45.     return True
  46.  
  47. def is_palindrome(n):
  48.     """
  49.    Check if a number is a palindrome.
  50.  
  51.    Parameters:
  52.    - n (int): The number to check.
  53.  
  54.    Returns:
  55.    - bool: True if the number is a palindrome, False otherwise.
  56.    """
  57.     return str(n) == str(n)[::-1]
  58.  
  59. def find_prime_palindromes(start, end):
  60.     """
  61.    Find prime palindromes within a given range.
  62.  
  63.    Parameters:
  64.    - start (int): The starting number of the range.
  65.    - end (int): The ending number of the range.
  66.  
  67.    Returns:
  68.    - list: A list of prime palindromes found within the range.
  69.    """
  70.     primes = []
  71.     for num in range(max(11, start), end):
  72.         if is_prime(num) and is_palindrome(num):
  73.             primes.append(num)
  74.     return primes
  75.  
  76.  
  77. def save_prime_palindromes(primes, range_start, range_end, max_end):
  78.     """
  79.    Save prime palindromes to a file.
  80.  
  81.    Parameters:
  82.    - primes (list): List of Prime Palindromes to save.
  83.    - range_start (int): The starting number of the range.
  84.    - range_end (int): The ending number of the range.
  85.    - max_end (int): The maximum value of the range.
  86.  
  87.    Returns:
  88.    - None
  89.    """
  90.     if primes:
  91.         num_zeros = len(str(max_end))
  92.         padded_range_start = str(range_start).zfill(num_zeros)
  93.         padded_range_end = str(range_end).zfill(num_zeros)
  94.         file_name = f"prime_palindromes_{padded_range_start}_{padded_range_end}.txt"
  95.         with open(file_name, "w") as file:
  96.             for prime in primes:
  97.                 file.write(str(prime) + "\n")
  98.         print(f"\nSaved Prime Palindromes in range {padded_range_start}-{padded_range_end} to {file_name}")
  99.     else:
  100.         print(f"\nNo palindromic Primes found in range {range_start}-{range_end}")
  101.  
  102. def should_skip_range(range_start):
  103.     """
  104.    Check if a range should be skipped based on its starting number.
  105.  
  106.    Parameters:
  107.    - range_start (int): The starting number of the range.
  108.  
  109.    Returns:
  110.    - bool: True if the range should be skipped, False otherwise.
  111.    """
  112.     return str(range_start)[0] in {'2', '3', '4', '5', '6', '8'}
  113.  
  114. if __name__ == "__main__":  # Minimum range_start=11 (Max determined by system capacity)
  115.     start_range = 11
  116.     end_range = 1000000000000
  117.            
  118.     print("\nFinding and saving prime palindromes...")
  119.     max_end = end_range
  120.     for i in range(start_range, end_range, 1000000):
  121.         if should_skip_range(i):
  122.             continue
  123.         primes_in_range = find_prime_palindromes(i, i + 1000000)
  124.         save_prime_palindromes(primes_in_range, i, i + 999999, max_end)
  125.  
  126.     print("\nAll Prime Palindromes have been saved to files.\tGoodbye!")
  127.  
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement