jaytea commented on a Page, $isprime() and $iscomposite()  -  Apr 09, 2011

while the regex method is nifty, it isn't really one to be taken seriously. it is severely limited, especially given mIRC's line length limit (after all, who hasn't memorized prime numbers up to 4,150??) and the algorithm the regex engine applies is inefficient in principle, even with potential optimizations, though still destined to edge out mIRC script for any appropriately sized input.

and though it's not nearly as interesting to examine code size as code speed for this kind of task, you can of course further shorten those aliases:

alias isprime return $istok($regex($str(1,$1),^1?$|^(11+)\1+$),0,1)
alias iscomposite return $iif(!$isprime($1),) $v1

it is probably worth noting that all of these methods yield incorrect results for $iscomposite(0) and $iscomposite(1); 0 and 1 are neither prime nor composite.

TC, i'm sure you realize that such a snippet can't really be made to perform satisfactorily in a language such as mSL, but you could optimize your current algorithm by: starting %x at 3 and increasing it by 2 each iteration, and terminating the loop just after %x exceeds floor(sqrt($1))

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.