Re: [q-lang-users] Re: Optimizations and tweaks
Brought to you by:
agraef
From: Tim H. <q...@st...> - 2004-10-26 16:23:29
|
Dr....@t-... (Albert Graef) writes: > Tim Haynes wrote: >> def HASH = ref emptyhdict; >> public hashed F X; >> hashed F X = get HASH!(F,X) if member (get HASH) (F,X); >> = put HASH (update (get HASH) (F,X) Y) || Y >> where Y = F X otherwise; >> hashed isprime = hisprime; >> hisprime N = not (any ((=0).(N mod))) (primes (intsqrt N)) ; >> hashed primes = hprimes; >> hprimes N = [2] if N=2; = [2,3] if N=3; >> = [2,3|filter isprime [5,7..N]] otherwise; > > Hmm, when I run this code over here, the hashed function is apparently > never invoked. Very strange! THe performance was completely different from the unhashed version - I observed it staying all-but linear when the all-the-odds algorithm, or even the unhashed one, were showing increases, here.. And yes it seemed to be producing the right answers, for a few random spot-checks, too :) > In fact, with filter isprime [5000000..5000100] I even get a failed > condition: > > ! Error in conditional > 0> stdlib.q, line 134: filter isprime [5000011,5000012,5000013,...] ==> > [5000011|filter isprime [...]] if isprime 5000011 Very weird. I did actually see a glitch with one large number, so I'll have to go back and investigate it more... > I think the applications of hashed are the wrong way round (it's easy to > fall into this trap, it has happened to me, too). I've attached my > (hopefully corrected) version of the script to this post. This one *does* > invoke hashed now (verified with tbreak). > > I'm ready to add this version to the examples now (if you don't complain > -- better do that quickly, as I'm currently doing the final touches for Q > 6.0 ;-). If it's not too late, I'd like to hold back on that - I'm travelling atm and will be back home tomorrowish. Can run yours and mine through the mill again then. I'm particularly worried about those error-in-conditional problems... :/ ~Tim -- <http://spodzone.org.uk/> |