write a python script that finds all prime numbers smaller than 1000
- User
write a python script that finds all prime numbers smaller than 1000
Assistant
Here's a Python script that finds all prime numbers smaller than 1000 using the Sieve of Eratosthenes algorithm:
def find_primes(n): # Create a boolean array "is_prime[0..n]" and initialize # all entries it as true is_prime = [True] * (n + 1) is_prime[0] = is_prime[1] = False # Use Sieve of Eratosthenes to mark non-prime numbers as false for i in range(2, int(n ** 0.5) + 1): if is_prime[i]: # Update all multiples of i for j in range(i * i, n + 1, i): is_prime[j] = False # Create the list of prime numbers primes = [i for i in range(n + 1) if is_prime[i]] return primes # Find all primes smaller than 1000 primes = find_primes(999) # Print the results print(f"There are {len(primes)} prime numbers smaller than 1000:") print(primes)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.