code me a prime number shower python script
- User
code me a prime number shower python script
Assistant
Prime Number Generator in Python
Here's a Python script that generates and displays prime numbers up to a given limit:
def is_prime(n): """Check if a number is prime.""" if n <= 1: return False if n == 2: return True if n % 2 == 0: return False for i in range(3, int(n**0.5) + 1, 2): if n % i == 0: return False return True def display_primes(limit): """Display all prime numbers up to the given limit.""" print(f"Prime numbers up to {limit}:") primes = [] for num in range(2, limit + 1): if is_prime(num): primes.append(num) print(primes) print(f"Total primes found: {len(primes)}") def main(): print("Prime Number Generator") print("----------------------") while True: try: limit = int(input("Enter the upper limit (or 0 to exit): ")) if limit == 0: print("Exiting the program...") break if limit < 2: print("Please enter a number greater than 1.") continue display_primes(limit) except ValueError: print("Please enter a valid integer.") if __name__ == "__main__": main()How to Use This Script:
- Run the script in a Python environment
- Enter an upper limit when prompted
- The script will display all prime numbers up to that limit
- Enter 0 to exit the program
Features:
- Efficient prime checking using trial division up to the square root
- Input validation to handle non-integer inputs
- Option to exit the program
- Displays both the list of primes and the count
Example Output:
Prime Number Generator ---------------------- Enter the upper limit (or 0 to exit): 20 Prime numbers up to 20: [2, 3, 5, 7, 11, 13, 17, 19] Total primes found: 8You can modify the script to suit your specific needs, such as changing the output format or adding additional functionality.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.