Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: pp_in_range_11_to_1t.py
- # Author: Jeoi Reqi
- """
- Find and save Prime Palindromes within the range from 11 to 1 trillion.
- Requirements:
- - Python 3.x
- Usage:
- 1. Ensure you have Python 3.x installed on your system.
- 2. Run the script.
- Important Notes:
- - The script finds Prime Palindromes within the range from 11 to 1 trillion.
- - It skips values less than 11 since the first palindromic prime is 11.
- - Prime Palindromes found within each million-range are saved to separate text files.
- - The script may take some time to execute, especially for larger ranges.
- - Make sure you have sufficient disk space to save the output files.
- """
- def is_prime(n):
- """
- Check if a number is prime.
- Parameters:
- - n (int): The number to check.
- Returns:
- - bool: True if the number is prime, False otherwise.
- """
- if n <= 1:
- return False
- if n <= 3:
- return True
- if n % 2 == 0 or n % 3 == 0:
- return False
- i = 5
- while i * i <= n:
- if n % i == 0 or n % (i + 2) == 0:
- return False
- i += 6
- return True
- def is_palindrome(n):
- """
- Check if a number is a palindrome.
- Parameters:
- - n (int): The number to check.
- Returns:
- - bool: True if the number is a palindrome, False otherwise.
- """
- return str(n) == str(n)[::-1]
- def find_prime_palindromes(start, end):
- """
- Find prime palindromes within a given range.
- Parameters:
- - start (int): The starting number of the range.
- - end (int): The ending number of the range.
- Returns:
- - list: A list of prime palindromes found within the range.
- """
- primes = []
- for num in range(max(11, start), end):
- if is_prime(num) and is_palindrome(num):
- primes.append(num)
- return primes
- def save_prime_palindromes(primes, range_start, range_end, max_end):
- """
- Save prime palindromes to a file.
- Parameters:
- - primes (list): List of Prime Palindromes to save.
- - range_start (int): The starting number of the range.
- - range_end (int): The ending number of the range.
- - max_end (int): The maximum value of the range.
- Returns:
- - None
- """
- if primes:
- num_zeros = len(str(max_end))
- padded_range_start = str(range_start).zfill(num_zeros)
- padded_range_end = str(range_end).zfill(num_zeros)
- file_name = f"prime_palindromes_{padded_range_start}_{padded_range_end}.txt"
- with open(file_name, "w") as file:
- for prime in primes:
- file.write(str(prime) + "\n")
- print(f"\nSaved Prime Palindromes in range {padded_range_start}-{padded_range_end} to {file_name}")
- else:
- print(f"\nNo palindromic Primes found in range {range_start}-{range_end}")
- def should_skip_range(range_start):
- """
- Check if a range should be skipped based on its starting number.
- Parameters:
- - range_start (int): The starting number of the range.
- Returns:
- - bool: True if the range should be skipped, False otherwise.
- """
- return str(range_start)[0] in {'2', '3', '4', '5', '6', '8'}
- if __name__ == "__main__": # Minimum range_start=11 (Max determined by system capacity)
- start_range = 11
- end_range = 1000000000000
- print("\nFinding and saving prime palindromes...")
- max_end = end_range
- for i in range(start_range, end_range, 1000000):
- if should_skip_range(i):
- continue
- primes_in_range = find_prime_palindromes(i, i + 1000000)
- save_prime_palindromes(primes_in_range, i, i + 999999, max_end)
- print("\nAll Prime Palindromes have been saved to files.\tGoodbye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement