Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: palindromic_primes_finder_v2.08.py
- # Author: Jeoi Reqi
- """
- Palindromic Primes Finder:
- This optimized version is approximately 60.47% faster than the version 2.07!
- This Python script exclusively focuses on identifying prime numbers that are palindromes.
- It utilizes the equation [P = y^2 - y + 41], where 'y' is provided by the user.
- The script iterates through a sequence of numbers, checking for palindromic primes, and displays the results.
- **Requirements:**
- - Python 3
- **Usage:**
- 1. Run the script in a terminal or command prompt.
- 2. Enter the starting value for 'y'.
- 3. Specify the number of iterations to check.
- 4. The script will output prime numbers that are also palindromes along with their corresponding 'y' values.
- **Note:**
- - The equation used is [P = y^2 - y + 41].
- - Only prime numbers that are palindromes will be displayed in the output.
- Example:
- Enter the starting value for y: 41
- Enter the number of iterations: 10000000
- Results:
- [y=131, p=17071]
- [y=1814, p=3288823]
- [y=2738, p=7493947]
- [y=2813, p=7910197]
- [y=12176, p=148242841]
- [y=13951, p=194616491]
- [y=17692, p=312989213]
- [y=281233, p=79091719097]
- [y=1372760, p=1884468644881]
- [y=1956282, p=3827037307283]
- Processing time: 0 hours, 0 minutes, 7.526364 seconds
- """
- import time
- def is_prime(num):
- """
- Checks if a given number is prime.
- Args:
- num (int): The number to check for primality.
- Returns:
- bool: True if the number is prime, False otherwise.
- """
- if num < 2:
- return False
- if num == 2 or num == 3:
- return True
- if num % 2 == 0 or num % 3 == 0:
- return False
- for i in range(5, int(num**0.5) + 1, 6):
- if num % i == 0 or num % (i + 2) == 0:
- return False
- return True
- def is_palindrome(num):
- """
- Checks if a given number is a palindrome.
- Args:
- num (int): The number to check for palindrome.
- Returns:
- bool: True if the number is a palindrome, False otherwise.
- """
- str_num = str(num)
- return str_num == str_num[::-1]
- def check_palindrome_prime(y):
- """
- Checks if a given 'y' produces a palindromic prime.
- Args:
- y (int): The 'y' value.
- Returns:
- tuple: A tuple (y, P) if 'y' produces a palindromic prime, None otherwise.
- """
- P = y**2 - y + 41
- return (y, P) if is_palindrome(P) else None
- def generate_primes_and_palindromes(start_y, iterations):
- """
- Generates and identifies prime and palindromic prime numbers.
- Args:
- start_y (int): The starting value for 'y'.
- iterations (int): The number of iterations to check.
- Returns:
- None
- """
- results = []
- start_time = time.time()
- for y in range(start_y, start_y + iterations):
- result = check_palindrome_prime(y)
- if result:
- results.append(result)
- print("\nResults:")
- if not results:
- print("No Palindromic Primes found.")
- else:
- for result in results:
- print(f"[y={result[0]}, p={result[1]}]")
- end_time = time.time()
- elapsed_time = end_time - start_time
- hours, remainder = divmod(elapsed_time, 3600)
- minutes, seconds = divmod(remainder, 60)
- print(f"\nProcessing time: {int(hours)} hours, {int(minutes)} minutes, {seconds:.6f} seconds\n")
- if __name__ == "__main__":
- start_y = int(input("Enter the starting value for y: "))
- iterations = int(input("Enter the number of iterations: "))
- generate_primes_and_palindromes(start_y, iterations)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement