Topic: https://russell.ballestrini.net/explaining-cache-with-python/
hide preview

What's next? verify your email address for reply notifications!

unverified 7y, 108d ago [edited]

instead of writing this line

for i in range( 2, x ):

Can't we use the cache to build a prime list for our advantage?

for i in primes_list:

remark link
hide preview

What's next? verify your email address for reply notifications!

russell 7y, 107d ago [edited]

In this case the cache is built using a python dictionary. We can check this dictionary first before computing whether a given number is prime.

for i in range(2, x):

This syntax counts from 2 to x-1 and assigns the number to i. We have to do this to test if x is ever divisible by i without a remainder. If we ever modulus x by i and get 0 (no remainder) then x is not prime.

hide preview

What's next? verify your email address for reply notifications!

russell 7y, 107d ago [edited]

Update - My new laptop is significantly faster at computing primes:

>>> from time import time
>>> from primer import Primer
>>> p = Primer()
>>> s1 = time();p.is_prime( 97352613 );e1 = time()
False
>>> s2 = time();p.is_prime( 97352613 );e2 = time()
False
>>> e1 - s1
2.10681414604187
>>> e2 - s2
0.01556706428527832
hide preview

What's next? verify your email address for reply notifications!

unverified 4y, 296d ago

thanks

hide preview

What's next? verify your email address for reply notifications!