Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: py2y41_all.py
- # Author: Jeoi Reqi
- """
- This script generates and analyzes Prime numbers and Palindromic Primes using the Ulam Spiral pattern.
- Description:
- The program calculates Prime numbers & 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.
- Primes are indicated with 'yellow' text, Palindromic Primes with 'red' text & all Non-Prime with 'black' text in the terminal.
- 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 prime numbers, palindromic primes & Non-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):
- if is_palindrome(y):
- # Red for palindromic primes
- results.append(f"\033[31m[y={y}, pP={P}] [Is Palindromic Prime]\033[0m")
- else:
- # Yellow for primes that are not palindromes
- results.append(f"\033[33m[y={y}, P={P} ] [Is Prime]\033[0m")
- else:
- # Black for all Non-Prime numbers
- results.append(f"[y={y}, nP={P}] [Not Prime]")
- 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:")
- 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