Advertisement
Python253

py2y41_all

Mar 11th, 2024
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: py2y41_all.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. This script generates and analyzes Prime numbers and Palindromic Primes using the Ulam Spiral pattern.
  8.  
  9. Description:
  10. The program calculates Prime numbers & 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. Primes are indicated with 'yellow' text, Palindromic Primes with 'red' text & all Non-Prime with 'black' text in the terminal.
  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 prime numbers, palindromic primes & Non-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):
  45.             if is_palindrome(y):
  46.                 # Red for palindromic primes
  47.                 results.append(f"\033[31m[y={y}, pP={P}]   [Is Palindromic Prime]\033[0m")
  48.             else:
  49.                 # Yellow for primes that are not palindromes
  50.                 results.append(f"\033[33m[y={y}, P={P} ]   [Is Prime]\033[0m")
  51.         else:
  52.             # Black for all Non-Prime numbers
  53.             results.append(f"[y={y}, nP={P}]   [Not Prime]")
  54.  
  55.     return results
  56.  
  57. def main():
  58.     start_time = time.process_time()
  59.  
  60.     start_y = int(input("Enter the starting value for y: "))
  61.     iterations = int(input("Enter the number of iterations: "))
  62.     data = generate_primes_and_palindromes(start_y, iterations)
  63.  
  64.     end_time = time.process_time()
  65.     elapsed_time = end_time - start_time
  66.  
  67.     print("\nResults:")
  68.     for result in data:
  69.         print(result)
  70.  
  71.     # Print the elapsed time in hours, minutes, and seconds format
  72.     hours, remainder = divmod(elapsed_time, 3600)
  73.     minutes, seconds = divmod(remainder, 60)
  74.     print(f"\nTime elapsed: {int(hours)} hours, {int(minutes)} minutes, {seconds:.2f} seconds\n")
  75.  
  76. if __name__ == "__main__":
  77.     main()
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement