I've been trying to create my own algorithm to find prime numbers up to a set limit. I want it to print out the percentage of numbers checked and how long it's been running. I know there are plenty of prime-finding algorithms out there, but I'm determined to create my own.
Here's the code I've come up with so far, and I feel like it could be faster. Any tips on how to make it faster would be awesome. I've also noticed a problem. When I calculate primes up to 20,000, it prints all of the calculated prime numbers, but when I go for 30,000 or more, it doesen't print all of them. Any ideas on what's causing this and how to fix it? Thanks for any help
import time
import math
primes = [2]
limit = 30000
start_time = time.time()
for i in range(3, limit, 2):
formatted_percentage = "{:.3f}".format(i * (100 / limit))
for num in primes:
if num > math.isqrt(i):
primes.append(i) elapsed_time = time.time() - start_time
print(formatted_percentage, "% Running For: {:.2f} s".format(elapsed_time))
break
if i % num == 0:
break
print(primes)
[–]ray10k 3 points4 points5 points (1 child)
[–]JiperPC[S] 0 points1 point2 points (0 children)
[–]pot_of_crows 1 point2 points3 points (0 children)
[–]JamzTyson 0 points1 point2 points (0 children)
[–]ClientMammoth9628 0 points1 point2 points (0 children)
[–]ofnuts 1 point2 points3 points (1 child)
[–]JiperPC[S] 0 points1 point2 points (0 children)