Re: [q-lang-users] Re: Optimizations and tweaks
Brought to you by:
agraef
|
From: Tim H. <q...@st...> - 2004-10-12 09:59:14
|
Dr....@t-... (Albert Graef) writes:
> Tim Haynes wrote:
>> Oh, *excellent* <rubs hands in glee>. Thanks for that. Refs as well? I have
>> much Q to be learning. :)
>
> Also soon to be unleashed upon the unexpecting world: SWIG support! This
> will turn the creation of library interfaces into a no-brainer. But don't
> tell anyone just yet, I'm still testing. ;-) C already works very well, I
> wish I had that when I wrote the clib module ... As soon as I'm satisfied
> with the result of auto-wrapping a huge C++ library like the entire Qt
> it's time for a release again.
Oh wow. If it's really capable of coping with something that size, it'll be
amazing. I'm already wondering about writing a tiny graphical app in Q (an
RSS aggregator with a difference) as it is :)
>> I've implemented memoization on `prime' and `isprime' functions - works
>> first time ;)
>
> Care to post your final algorithm here?
Sure. :) I think this is the best of all worlds so far:
#!/usr/bin/env q
#! -q
#! -cmain ARGS || quit
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;
def P=val (ARGS!1);
dump N = strcat [str N, " is ", (str (isprime N)), "\n"];
main ARGS = dump P;
(The reason for `dump' and main being separate is that I was running dump,
and therefore isprime, across a list [(P-10)..(P+10)], for benchmarking
purposes.. :)
You're welcome to use it for docs / consider it GPL'd / whatever's
easiest, as you wish. :)
~Tim
--
<http://spodzone.org.uk/>
|