Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Answering this SO question this SO question recently, I've developed the following code (based on a well-known Active Code recipe, as discussed here):

wheel = [2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,
 4,8,6,4,6,2,4,6,2,6,6,4,2,4,6,2,6,4,2,4,2,10,2,10]
wsize = 48
 
def primes(): 
 yield from (2, 3, 5, 7)
 yield from wsieve()
 
def wsieve(): # wheel-sieve, by Will Ness. cf. ideone.com/WFv4f
 yield 11 # cf. httphttps://stackoverflow.com/a/10733621/849891 
 mults = {} # httphttps://stackoverflow.com/a/19391111/849891
 ps = wsieve() 
 p = next(ps) 
 psq, c, i = p*p, 11, 0 # 13 = 11 + wheel[0]
 cbase, ibase = 11, 0 
 while True:
 c += wheel[i] ; i = (i+1) % wsize # 17 = 13 + wheel[1]
 if c in mults:
 (j,pbase) = mults.pop(c) # add(mults, NEXT c, j, pbase)
 elif c < psq: 
 yield c ; continue 
 else: # (c==psq) 
 while not cbase == p:
 cbase += wheel[ibase]
 ibase = (ibase+1) % wsize # ibase - initial offset into wheel, for p
 j, pbase = ibase, p # add(mults, NEXT c, ibase, p)
 p = next(ps) ; psq = p*p
 m = c + pbase*wheel[j] ; j = (j+1) % wsize # mults(p) = map (*p)
 while m in mults: # roll(wheel,ibase,p)
 m += pbase*wheel[j] ; j = (j+1) % wsize 
 mults[m] = (j,pbase) 

In particular, I have the rolled wheel streams inlined, using different variables for different "objects". It seems all over the place; is there a way to encapsulate it into a bona fide object, without loosing performance? (last time last time when I used functions for some encapsulation, efficiency suffered efficiency suffered).

Answering this SO question recently, I've developed the following code (based on a well-known Active Code recipe, as discussed here):

wheel = [2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,
 4,8,6,4,6,2,4,6,2,6,6,4,2,4,6,2,6,4,2,4,2,10,2,10]
wsize = 48
 
def primes(): 
 yield from (2, 3, 5, 7)
 yield from wsieve()
 
def wsieve(): # wheel-sieve, by Will Ness. cf. ideone.com/WFv4f
 yield 11 # cf. http://stackoverflow.com/a/10733621/849891 
 mults = {} # http://stackoverflow.com/a/19391111/849891
 ps = wsieve() 
 p = next(ps) 
 psq, c, i = p*p, 11, 0 # 13 = 11 + wheel[0]
 cbase, ibase = 11, 0 
 while True:
 c += wheel[i] ; i = (i+1) % wsize # 17 = 13 + wheel[1]
 if c in mults:
 (j,pbase) = mults.pop(c) # add(mults, NEXT c, j, pbase)
 elif c < psq: 
 yield c ; continue 
 else: # (c==psq) 
 while not cbase == p:
 cbase += wheel[ibase]
 ibase = (ibase+1) % wsize # ibase - initial offset into wheel, for p
 j, pbase = ibase, p # add(mults, NEXT c, ibase, p)
 p = next(ps) ; psq = p*p
 m = c + pbase*wheel[j] ; j = (j+1) % wsize # mults(p) = map (*p)
 while m in mults: # roll(wheel,ibase,p)
 m += pbase*wheel[j] ; j = (j+1) % wsize 
 mults[m] = (j,pbase) 

In particular, I have the rolled wheel streams inlined, using different variables for different "objects". It seems all over the place; is there a way to encapsulate it into a bona fide object, without loosing performance? (last time when I used functions for some encapsulation, efficiency suffered).

Answering this SO question recently, I've developed the following code (based on a well-known Active Code recipe, as discussed here):

wheel = [2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,
 4,8,6,4,6,2,4,6,2,6,6,4,2,4,6,2,6,4,2,4,2,10,2,10]
wsize = 48
 
def primes(): 
 yield from (2, 3, 5, 7)
 yield from wsieve()
 
def wsieve(): # wheel-sieve, by Will Ness. cf. ideone.com/WFv4f
 yield 11 # cf. https://stackoverflow.com/a/10733621/849891 
 mults = {} # https://stackoverflow.com/a/19391111/849891
 ps = wsieve() 
 p = next(ps) 
 psq, c, i = p*p, 11, 0 # 13 = 11 + wheel[0]
 cbase, ibase = 11, 0 
 while True:
 c += wheel[i] ; i = (i+1) % wsize # 17 = 13 + wheel[1]
 if c in mults:
 (j,pbase) = mults.pop(c) # add(mults, NEXT c, j, pbase)
 elif c < psq: 
 yield c ; continue 
 else: # (c==psq) 
 while not cbase == p:
 cbase += wheel[ibase]
 ibase = (ibase+1) % wsize # ibase - initial offset into wheel, for p
 j, pbase = ibase, p # add(mults, NEXT c, ibase, p)
 p = next(ps) ; psq = p*p
 m = c + pbase*wheel[j] ; j = (j+1) % wsize # mults(p) = map (*p)
 while m in mults: # roll(wheel,ibase,p)
 m += pbase*wheel[j] ; j = (j+1) % wsize 
 mults[m] = (j,pbase) 

In particular, I have the rolled wheel streams inlined, using different variables for different "objects". It seems all over the place; is there a way to encapsulate it into a bona fide object, without loosing performance? (last time when I used functions for some encapsulation, efficiency suffered).

edited tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
edited tags
Link
Will Ness
  • 1k
  • 1
  • 8
  • 25
added 74 characters in body
Source Link
Will Ness
  • 1k
  • 1
  • 8
  • 25
Loading
deleted 824 characters in body
Source Link
Will Ness
  • 1k
  • 1
  • 8
  • 25
Loading
Source Link
Will Ness
  • 1k
  • 1
  • 8
  • 25
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /