All prime numbers > 3 have either the form 6k+5 (k>=0) or 6k+1 (k>=1). Therefore you can filter the "numbers beyond all primes list", and you get rid of 2/3 of occurrences. I tested many prime programs in python and i have many ideas. I can generate all primes up to 100000 in 0.022 secs and detect primes of 17 digits in only 7.23 secs.
I suggest doing something slightly different, too. Instead of keeping a list of old primes, use sets instead. You can keep track of your largest and most recent prime as a separate variable. Using sets may speed things up a bit more since the access time is faster. But then again, if your primes are out of order this could hurt your time as well. Im not sure; havent tested this. There are more efficient ways of generating a list of primes, still. The Sieve of Eratosthenes can be coded in Python fairly efficiently and is much faster than these brute force generators. Even though I guess it is a kind of brute force generator in its own right, it takes a slightly different approach. As for checking for the primality of a specific integer, there are much more efficient algorithms such as the Miller-Rabin test, and others. If you really want to have a coding adventure in python to learn a lot about math and primes and efficient coding, try building integer factorization algorithms. Talk about a challenge! The theory and the practice are lightyears above.
def sieve_of_atkin(limit): P = [2, 3] sieve = [False] * (limit + 1) for x in range(1, int(limit**0.5) + 1): for y in range(1, int(limit**0.5) + 1): n = 4 * x**2 + y**2 if n
All prime numbers > 3 have either the form 6k+5 (k>=0) or 6k+1 (k>=1). Therefore you can filter the "numbers beyond all primes list", and you get rid of 2/3 of occurrences. I tested many prime programs in python and i have many ideas. I can generate all primes up to 100000 in 0.022 secs and detect primes of 17 digits in only 7.23 secs.
please share your code, I am exciting to try
I learned something new today. Thank you.
Could you mention your machine
I suggest doing something slightly different, too. Instead of keeping a list of old primes, use sets instead. You can keep track of your largest and most recent prime as a separate variable. Using sets may speed things up a bit more since the access time is faster. But then again, if your primes are out of order this could hurt your time as well. Im not sure; havent tested this.
There are more efficient ways of generating a list of primes, still. The Sieve of Eratosthenes can be coded in Python fairly efficiently and is much faster than these brute force generators. Even though I guess it is a kind of brute force generator in its own right, it takes a slightly different approach.
As for checking for the primality of a specific integer, there are much more efficient algorithms such as the Miller-Rabin test, and others.
If you really want to have a coding adventure in python to learn a lot about math and primes and efficient coding, try building integer factorization algorithms. Talk about a challenge! The theory and the practice are lightyears above.
Dude that's better than expected
nice work ... primes are underrated lol
damn, pretty nice
Confused 😂😂😂 me.wow
efficient?
you only have to loop through the range 2,x//2+1
but 2 is a prime number
Good but speaking so fast
Sorry about that. You can slow down the video under settings through the gear icon on youtube.
def sieve_of_atkin(limit):
P = [2, 3]
sieve = [False] * (limit + 1)
for x in range(1, int(limit**0.5) + 1):
for y in range(1, int(limit**0.5) + 1):
n = 4 * x**2 + y**2
if n
Fastest algorithm
wonderful !! :)