Advertisement
Python253

py2y41_pal

Mar 11th, 2024
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: py2y41_pal.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. This script generates and analyzes Palindromic Primes using the Ulam Spiral pattern.
  8.  
  9. Description:
  10. The program calculates Palindromic Primes based on the Ulam Spiral pattern, following the equation [P = y^2 - y + 41].
  11. The user is prompted to input the starting value 'y' and the number of iterations.
  12. Palindromic Primes are output to the terminal with 'red' text.
  13.  
  14. Requirements:
  15. - Python 3
  16. - User input for starting value 'y' and the number of iterations
  17. - Utilizes ANSI escape codes for colored output in the terminal
  18.  
  19. Usage:
  20. 1. Run the script in a terminal or command prompt.
  21. 2. Enter the starting value for 'y' and the number of iterations.
  22. 3. View the results displaying Palindromic Primes with colorful annotations.
  23. 4. The script prints the elapsed time in hours, minutes, and seconds.
  24. """
  25.  
  26. import time
  27.  
  28. def is_prime(num):
  29.     if num < 2:
  30.         return False
  31.     for i in range(2, int(num**0.5) + 1):
  32.         if num % i == 0:
  33.             return False
  34.     return True
  35.  
  36. def is_palindrome(num):
  37.     return str(num) == str(num)[::-1]
  38.  
  39. def generate_primes_and_palindromes(start_y, iterations):
  40.     results = []
  41.     for y in range(start_y, start_y + iterations):
  42.         P = y**2 - y + 41
  43.  
  44.         if is_prime(y) and is_palindrome(y):
  45.             # Red For Palindromic Primes
  46.             results.append(f"\033[31m[y={y}, pP={P}]   [Is Palindromic Prime]\033[0m")
  47.         else:
  48.             # Excludes Non-Palindromic Prime & Non-Prime Numbers From Output
  49.             pass
  50.  
  51.     return results
  52.  
  53. def main():
  54.     start_time = time.process_time()
  55.  
  56.     start_y = int(input("Enter the starting value for y: "))
  57.     iterations = int(input("Enter the number of iterations: "))
  58.     data = generate_primes_and_palindromes(start_y, iterations)
  59.  
  60.     end_time = time.process_time()
  61.     elapsed_time = end_time - start_time
  62.  
  63.     print("\nResults:")
  64.     if not data:
  65.         print("No Palindromic Primes found.")
  66.     else:
  67.         for result in data:
  68.             print(result)
  69.  
  70.     # Print the elapsed time in hours, minutes, and seconds format
  71.     hours, remainder = divmod(elapsed_time, 3600)
  72.     minutes, seconds = divmod(remainder, 60)
  73.     print(f"\nTime elapsed: {int(hours)} hours, {int(minutes)} minutes, {seconds:.2f} seconds\n")
  74.  
  75. if __name__ == "__main__":
  76.     main()
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement