Re: detecting primes - bugfix
Re: detecting primes - bugfix
- Subject: Re: detecting primes - bugfix
- From: ThK <email@hidden>
- Date: Fri, 23 Feb 2001 11:06:46 +0100
On 22.02.2001 at 18:55 Uhr, John S. Baltutis <email@hidden> wrote:
>
>
*****
>
I beg to differ! 4, 6, 8, and 9 are NOT prime!
>
Sure, i will correct this. Sorry, i was a bit late before sleeping. Here is the corrected script:
-- Detecting prime numbers from 1 to 100 - (Prime number: can be divided only by 1 or by itself )
-- 0 is not allowed because it is not a number!
repeat with x from 0 to 100
log {x, isItaPrime(x)}
end repeat
on isItaPrime(p)
if p = 0 then return "0 is not a number!"
if p > 3 then -- Numbers 1 and 2 are prime numbers.
repeat with z from 2 to 9 -- If a number can be divided by another number it's not a prime!
if p z then
set r to p / z
set isPrime to false
try
set r to r as integer -- Hey! If it works, the number was a multiple of z -> not a prime
set isPrime to false -- return this!
exit repeat -- and leave the loop!
on error
set isPrime to true -- The number can only be devided by itself.
--If this is a fact with all "z"'s, this is the last result in the loop that will be returned!
end try
else
return true
end if
end repeat
else
set isPrime to true -- The number was between 0 and 9 and is a prime!
end if
return isPrime
end isItaPrime
OK?
Thanks for bugreporting.
Thomas
_______________________________________
Thomas Johannes Matthias Kuehner
Developer
AppleScripter
Administrator
Webmaster
macgix - software,network,consulting
http://www.macgix.com
http://www.ScriptMyMac.de