[REBOL] Re: Speed testing prime functions..
From: joel:neely:fedex at: 28-Nov-2001 13:28
Oooops... (I *said* I was under the weather! ;-)
Joel Neely wrote:
Since 2 is prime, an initial bailout check is needed for that case
(as I *did* catch for 3 in the fourth round of optimization)...
> 2) After testing 2...
>
> prime?: func [p /local c] [
if p = 2 [return true]
> if p // 2 <> 0 [
> for c 3 (p - 1) 2 [
> if p // c = 0 [return false]
> ]
> return true
> ]
> ]
>
> 3) Consider the fact that a divisor less than the square root of...
>
> prime?: func [p /local c] [
if p = 2 [return true]
> if p // 2 <> 0 [
> c: 3
> while [c * c <= p] [
> if p // c = 0 [return false]
> c: c + 2
> ]
> return true
> ]
> ]
>
> 4) After testing 3, there's no point in checking for any other...
>
> prime?: func [p /local c s] [
if p = 2 [return true]
> if p // 2 <> 0 [
> if p = 3 [return true] ;; below only works for >= 5
> if p // 3 <> 0 [
> c: 5 s: 4
> while [c * c <= p] [
> if p // c = 0 [return false]
> c: c + s: 6 - s
> ]
> return true
> ]
> ]
> ]
>
-jn-
--
; sub REBOL {}; sub head ($) {@_[0]}
REBOL []
# despam: func [e] [replace replace/all e ":" "." "#" "@"]
; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"}
print head reverse despam "moc:xedef#yleen:leoj" ;