gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
Context:
- User
Assistant: User: Context: User: how to make a prime number calculator in python Assistant: Assistant:
- Assistant
To create a prime number calculator in Python, you can write a simple function that determines whether a given number is prime. A prime number is a natural number greater than 1 that is not divisible by any other numbers except 1 and itself. Here's a straightforward way to implement this logic in Python:
def is_prime(n): """Check if a number is prime.""" # Check for less than 2, 2 and 3 cases if n <= 1: return False if n <= 3: return True # Eliminate multiples of 2 and 3 if n % 2 == 0 or n % 3 == 0: return False # Try dividing n by numbers from 5 to sqrt(n) i = 5 while i * i <= n: if n % i == 0 or n % (i + 2) == 0: return False i += 6 return True def find_primes_up_to(limit): """Find and return a list of all prime numbers up to a specified limit.""" primes = [] for num in range(2, limit + 1): if is_prime(num): primes.append(num) return primes # Example usage: number = 29 if is_prime(number): print(f"{number} is a prime number.") else: print(f"{number} is not a prime number.") # Find all prime numbers up to a given limit limit = 100 primes = find_primes_up_to(limit) print(f"Prime numbers up to {limit}: {primes}")