0

I am using numpy, and sagemath for next_prime(). Could i get significant boosts from using a lower level language? Any suggestions would be greatly appreciated.

length = 99
width= 9
primeArray = np.empty(length,dtype=int)
primeArray[0]=2
for i in xrange(1,length-1):
   primeArray[i]=next_prime(primeArray[i-1])
primeMods = primeArray[1:width+1]
primeModArray =np.empty([length,width],dtype=int)
for x in xrange(width):
    for y in xrange(length):
        primeModArray[y][x] = primeArray[y]
output = np.mod(primeModArray,primeMods)
Greg
  • 1
  • 1
    check [this](https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n) – MaxU May 26 '17 at 22:53
  • I would just do it all in Sage, personally, as it would have arbitrary (up to your memory) sized primes. – kcrisman May 26 '17 at 23:22
  • Do you know if sage has a vectorized mod function like numpy? – Greg May 27 '17 at 00:09
  • Using prime_range instead of next_prime might help. – William Stein May 27 '17 at 20:21
  • Thanks William. I added a seive and it helped a lot. I will try Sage's primerange when i get home. It didn't come up when i searched the documentation for a sieve. Now the computation of mods dominates prime generation with just the first 10 mods. I guess i need to find a faster mod function and creat a numpy ufunc with it. The sage support said "Maybe you're looking for a matrix over a finite field? http://doc.sagemath.org/html/en/reference/finite_rings/sage/rings/finite_rings/finite_field_constructor.html also look at the matrix( ... ...) function." i will try one of those . Any tips? – Greg May 28 '17 at 21:59
  • Actually it is the duplication of the prime array that takes all the time even with just 19 copies – Greg May 29 '17 at 01:21
  • primeArray=np.array(prime_range(limit)) length= primeArray.shape[0] time1 = time.time() primeMods = primeArray[1:width+1] primeModArray =np.empty([length,width]) for x in xrange(width): for y in xrange(length): primeModArray[y][x] = primeArray[y] time2 = time.time() output = np.mod(primeModArray,primeMods) time3 = time.time() print output print primeModArray.shape print time1-time0 print time2-time1 print time3-time2 (664579, 19) 0.345091104507 21.3867900372 0.969210863113 – Greg May 29 '17 at 01:23
  • sorry. point is it takes .34 seconds to generate 664k primes, 21 seconds to make 19 copies and only 1 second to calculate 6664k * 19 mods – Greg May 29 '17 at 01:41

0 Answers0