Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: py2y41_pal.py
- # Author: Jeoi Reqi
- """
- This script generates and analyzes Palindromic Primes using the Ulam Spiral pattern.
- Description:
- The program calculates Palindromic Primes based on the Ulam Spiral pattern, following the equation [P = y^2 - y + 41].
- The user is prompted to input the starting value 'y' and the number of iterations.
- Palindromic Primes are output to the terminal with 'red' text.
- Requirements:
- - Python 3
- - User input for starting value 'y' and the number of iterations
- - Utilizes ANSI escape codes for colored output in the terminal
- Usage:
- 1. Run the script in a terminal or command prompt.
- 2. Enter the starting value for 'y' and the number of iterations.
- 3. View the results displaying Palindromic Primes with colorful annotations.
- 4. The script prints the elapsed time in hours, minutes, and seconds.
- """
- import time
- def is_prime(num):
- if num < 2:
- return False
- for i in range(2, int(num**0.5) + 1):
- if num % i == 0:
- return False
- return True
- def is_palindrome(num):
- return str(num) == str(num)[::-1]
- def generate_primes_and_palindromes(start_y, iterations):
- results = []
- for y in range(start_y, start_y + iterations):
- P = y**2 - y + 41
- if is_prime(y) and is_palindrome(y):
- # Red For Palindromic Primes
- results.append(f"\033[31m[y={y}, pP={P}] [Is Palindromic Prime]\033[0m")
- else:
- # Excludes Non-Palindromic Prime & Non-Prime Numbers From Output
- pass
- return results
- def main():
- start_time = time.process_time()
- 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)
- end_time = time.process_time()
- elapsed_time = end_time - start_time
- print("\nResults:")
- if not data:
- print("No Palindromic Primes found.")
- else:
- for result in data:
- print(result)
- # Print the elapsed time in hours, minutes, and seconds format
- hours, remainder = divmod(elapsed_time, 3600)
- minutes, seconds = divmod(remainder, 60)
- print(f"\nTime elapsed: {int(hours)} hours, {int(minutes)} minutes, {seconds:.2f} seconds\n")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement