Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: primes_finder_plus_optimized.py
- # Version: 2.01
- # Author: Jeoi Reqi
- import time
- """
- Primes Finder +Plus+:
- This Python script identifies prime & palindromic prime numbers generated using the equation [P = y^2 - y + 41], where 'y' is a starting value provided by the user.
- The script iterates through a sequence of numbers, checks if each corresponds to a prime and subsequently palindromic primes, and displays the results to the terminal.
- **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 primes & palindromic prime numbers along with their corresponding 'y' values.
- **Note:**
- - The equation used is [P = y^2 - y + 41].
- - Prime numbers that are also palindromes will be highlighted in the output.
- Example:
- Enter the starting value for y: 41
- Enter the number of iterations: 5000
- Results:
- [y=43, P=1847*]
- [y=44, P=1933*]
- [y=46, P=2111*]
- [y=47, P=2203*]
- [y=48, P=2297*]
- [y=49, P=2393*]
- ...
- [y=5034, P=25336163*]
- [y=5038, P=25376447*]
- [y=5040, P=25396601*]
- Processing time: 0 hours, 0 minutes, 0.406290 seconds
- """
- 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
- i = 5
- w = 2
- while i * i <= num:
- if num % i == 0:
- return False
- i += w
- w = 6 - w
- 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 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:
- list: A list of formatted strings representing the results.
- """
- results = []
- start_time = time.time()
- for y in range(start_y, start_y + iterations):
- P = y**2 - y + 41
- if is_prime(P) and is_palindrome(P):
- results.append(f"\033[31m[y={y}, pP={P}**]\033[0m")
- elif is_prime(P):
- results.append(f"[y={y}, P={P}*]")
- elif is_palindrome(P):
- results.append(f"\033[33m[y={y}, p={P}]\033[0m")
- end_time = time.time()
- elapsed_time = end_time - start_time
- hours, remainder = divmod(elapsed_time, 3600)
- minutes, seconds = divmod(remainder, 60)
- results.append(f"\nProcessing time: {int(hours)} hours, {int(minutes)} minutes, {seconds:.6f} seconds")
- return results
- def main():
- """
- Main function to execute the script.
- Takes user input for 'y' and iterations, then outputs the results to the terminal.
- """
- start_y = int(input("Enter the starting value for y: "))
- iterations = int(input("Enter the number of iterations: "))
- data = generate_primes_and_palindromes(start_y, iterations)
- print("\nResults:")
- for result in data:
- print(result)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement