Advertisement
Python253

primes_finder_plus_optimized_v2.01

Mar 11th, 2024 (edited)
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: primes_finder_plus_optimized.py
  4. # Version: 2.01
  5. # Author: Jeoi Reqi
  6.  
  7. import time
  8.  
  9. """
  10. Primes Finder +Plus+:
  11.  
  12. 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.
  13. 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.
  14.  
  15. **Requirements:**
  16. - Python 3
  17.  
  18. **Usage:**
  19. 1. Run the script in a terminal or command prompt.
  20. 2. Enter the starting value for 'y'.
  21. 3. Specify the number of iterations to check.
  22. 4. The script will output primes & palindromic prime numbers along with their corresponding 'y' values.
  23.  
  24. **Note:**
  25. - The equation used is [P = y^2 - y + 41].
  26. - Prime numbers that are also palindromes will be highlighted in the output.
  27.  
  28. Example:
  29. Enter the starting value for y: 41
  30. Enter the number of iterations: 5000
  31.  
  32. Results:
  33. [y=43, P=1847*]
  34. [y=44, P=1933*]
  35. [y=46, P=2111*]
  36. [y=47, P=2203*]
  37. [y=48, P=2297*]
  38. [y=49, P=2393*]
  39. ...
  40. [y=5034, P=25336163*]
  41. [y=5038, P=25376447*]
  42. [y=5040, P=25396601*]
  43.  
  44. Processing time: 0 hours, 0 minutes, 0.406290 seconds
  45. """
  46.  
  47. def is_prime(num):
  48.     """
  49.    Checks if a given number is prime.
  50.  
  51.    Args:
  52.        num (int): The number to check for primality.
  53.  
  54.    Returns:
  55.        bool: True if the number is prime, False otherwise.
  56.    """
  57.     if num < 2:
  58.         return False
  59.     if num == 2 or num == 3:
  60.         return True
  61.     if num % 2 == 0 or num % 3 == 0:
  62.         return False
  63.  
  64.     i = 5
  65.     w = 2
  66.  
  67.     while i * i <= num:
  68.         if num % i == 0:
  69.             return False
  70.         i += w
  71.         w = 6 - w
  72.  
  73.     return True
  74.  
  75. def is_palindrome(num):
  76.     """
  77.    Checks if a given number is a palindrome.
  78.  
  79.    Args:
  80.        num (int): The number to check for palindrome.
  81.  
  82.    Returns:
  83.        bool: True if the number is a palindrome, False otherwise.
  84.    """
  85.     str_num = str(num)
  86.     return str_num == str_num[::-1]
  87.  
  88. def generate_primes_and_palindromes(start_y, iterations):
  89.     """
  90.    Generates and identifies prime and palindromic prime numbers.
  91.  
  92.    Args:
  93.        start_y (int): The starting value for 'y'.
  94.        iterations (int): The number of iterations to check.
  95.  
  96.    Returns:
  97.        list: A list of formatted strings representing the results.
  98.    """
  99.     results = []
  100.     start_time = time.time()
  101.  
  102.     for y in range(start_y, start_y + iterations):
  103.         P = y**2 - y + 41
  104.  
  105.         if is_prime(P) and is_palindrome(P):
  106.             results.append(f"\033[31m[y={y}, pP={P}**]\033[0m")
  107.         elif is_prime(P):
  108.             results.append(f"[y={y}, P={P}*]")
  109.         elif is_palindrome(P):
  110.             results.append(f"\033[33m[y={y}, p={P}]\033[0m")
  111.  
  112.     end_time = time.time()
  113.     elapsed_time = end_time - start_time
  114.  
  115.     hours, remainder = divmod(elapsed_time, 3600)
  116.     minutes, seconds = divmod(remainder, 60)
  117.  
  118.     results.append(f"\nProcessing time: {int(hours)} hours, {int(minutes)} minutes, {seconds:.6f} seconds")
  119.  
  120.     return results
  121.  
  122. def main():
  123.     """
  124.    Main function to execute the script.
  125.    Takes user input for 'y' and iterations, then outputs the results to the terminal.
  126.    """
  127.     start_y = int(input("Enter the starting value for y: "))
  128.     iterations = int(input("Enter the number of iterations: "))
  129.     data = generate_primes_and_palindromes(start_y, iterations)
  130.  
  131.     print("\nResults:")
  132.     for result in data:
  133.         print(result)
  134.  
  135. if __name__ == "__main__":
  136.     main()
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement