Certainly! Below is a Python function that calculates and prints the number of prime numbers less than each integer x from 1 to 100. The function utilizes the Sieve of Eratosthenes algorithm, which is an efficient way to find all primes up to a specified limit.
Understanding the Sieve of Eratosthenes
The Sieve of Eratosthenes works by iteratively marking the multiples of each prime number starting from 2. The numbers that remain unmarked at the end of the process are prime.
Python Implementation
def count_primes_up_to_100():
max_limit = 100
# Initialize a list to track prime status of numbers from 0 to max_limit
is_prime = [True] * (max_limit + 1)
is_prime[0] = is_prime[1] = False # 0 and 1 are not primes
# Implementing Sieve of Eratosthenes
for current in range(2, int(max_limit**0.5) + 1):
if is_prime[current]:
for multiple in range(current*current, max_limit + 1, current):
is_prime[multiple] = False
# Precompute the cumulative count of primes up to each number
cumulative_primes = [0] * (max_limit + 1)
count = 0
for number in range(2, max_limit + 1):
if is_prime[number]:
count += 1
cumulative_primes[number] = count
# Print the number of primes less than each x from 1 to 100
print(f"{'x':>3} | Number of primes less than x")
print("-" * thirty)
for x in range(1, max_limit + 1):
if x > 0:
primes_less_than_x = cumulative_primes[x - 1] if x > 1 else 0
else:
primes_less_than_x = 0
print(f"{x:>3} | {primes_less_than_x}")
# Call the function to display the results
count_primes_up_to_100()
Explanation of the Code
-
Initialization:
max_limit is set to 100 since we're interested in numbers from 1 to 100.
is_prime is a list where each index represents whether the number is prime (True) or not (False). Initially, all numbers are assumed to be prime except for 0 and 1.
-
Sieve of Eratosthenes:
- Iterate from 2 up to the square root of
max_limit. For each prime number found, mark its multiples as non-prime.
- This efficiently eliminates all non-prime numbers.
-
Cumulative Count of Primes:
cumulative_primes stores the running total of prime numbers up to each index.
- As we iterate through each number, if it's prime, we increment the count and store it.
-
Displaying the Results:
- A formatted table is printed showing each
x from 1 to 100 alongside the number of primes less than x.
- Note that for
x = 1, there are 0 primes less than 1.
Sample Output
x | Number of primes less than x
------------------------------
1 | 0
2 | 0
3 | 1
4 | 2
5 | 2
6 | 3
7 | 3
8 | 4
9 | 4
10 | 4
...
100 | 25
(Only a portion of the output is shown for brevity.)
Running the Code
To execute the function:
- Copy the code into a Python environment (e.g., Python IDE, Jupyter Notebook, or an online Python compiler).
- Run the script.
- The output will display a table listing each number from 1 to 100 and the corresponding number of prime numbers less than that number.
Additional Notes
-
Efficiency: The Sieve of Eratosthenes is highly efficient for finding all primes up to a large number n. Its time complexity is approximately O(n log log n), making it suitable for this range.
-
Scalability: If you wish to check primes for numbers larger than 100, you can adjust the max_limit variable accordingly. However, for extremely large numbers, more optimized algorithms or segmented sieves might be necessary to handle memory and performance constraints.
-
Alternative Methods: While the sieve is efficient for this purpose, other methods like trial division can also be used but may not be as performant for larger ranges.
Feel free to modify and expand this function based on your specific requirements!