Find Prime Numbers Up to 1000 Using Python Prime numbers are the building blocks of number theory. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself (e.g., 2, 3, 5, 7, 11). Finding all prime numbers up to a certain limit, such as 1000, is a classic programming task that helps in understanding loops, conditionals, and algorithm optimization in Python.
In this article, we will explore two approaches to finding prime numbers up to 1000: A Simple Iterative Approach (Trial Division) The Efficient Sieve of Eratosthenes Method 1: The Simple Iterative Approach
The most straightforward way is to iterate through every number from 2 to 1000 and check if it is prime. To check if a number is prime, we try dividing it by all numbers from 2 up to nthe square root of n end-root
def find_primes_simple(limit): primes = [] for num in range(2, limit + 1): is_prime = True # Check for factors up to the square root of num for i in range(2, int(num0.5) + 1): if num % i == 0: is_prime = False break if is_prime: primes.append(num) return primes # Find primes up to 1000 prime_list = find_primes_simple(1000) print(prime_list) Use code with caution. How it works: We loop through num from 2 to 1000. For each num, we check if any number i from 2 up to numthe square root of n u m end-root divides it evenly (num % i == 0). If a divisor is found, it’s not prime. numthe square root of n u m end-root instead of num makes this method much faster. Method 2: The Sieve of Eratosthenes (Most Efficient)
For finding all primes up to a relatively small number like 1000, the Sieve of Eratosthenes is much faster. It works by creating a list of boolean values, initially assuming all numbers are prime, and then systematically marking multiples of each prime as not prime.
def sieve_of_eratosthenes(limit): # Create a boolean list “prime[0..limit]” and initialize # all entries it as true. sieve = [True] * (limit + 1) p = 2 while (p * p <= limit): # If sieve[p] is not changed, then it is a prime if (sieve[p] == True): # Update all multiples of p for i in range(p * p, limit + 1, p): sieve[i] = False p += 1 # Collect all prime numbers primes = [p for p in range(2, limit + 1) if sieve[p]] return primes # Find primes up to 1000 prime_list_sieve = sieve_of_eratosthenes(1000) print(prime_list_sieve) Use code with caution. Why this is better: It avoids performing division operations entirely.
It marks non-primes (multiples) rather than testing each number individually, reducing the complexity to Summary of Primes Up to 1000
Both methods will yield the same 168 prime numbers. The first few are:2, 3, 5, 7, 11, 13, 17, 19, 23, 29, … 997
For a simple script, the iterative method is fine, but for larger ranges, the Sieve of Eratosthenes is the preferred algorithm.
If you’d like, I can show you how to measure the time difference between these two methods, or help you apply this to even larger numbers. Let me know! Print series of prime numbers in python – Stack Overflow
Method 1 as described by Igor Chubin: def primes_method1(n): out = list() for num in range(1, n+1): prime = True for i in range(2, Stack Overflow How to generate Prime Numbers between 1 and 1,000 in Python