Re: magix number? - Yes, it is a prime number!
Re: magix number? - Yes, it is a prime number!
- Subject: Re: magix number? - Yes, it is a prime number!
- From: ThK <email@hidden>
- Date: Fri, 23 Feb 2001 01:58:06 +0100
Dear Scripters,
some more stuff about the classes REAL and INTEGER or "How to use them to detect prime numbers" (see the script at the end):
>
>
Take care of classes, please! It's not a question of math.
What i ment is this:
61 is a prime. The algorithms working with the formula in our compiled script may be different from them with a non prime number in our microprocessor.
Thr microprocessor calculates by writing values into registers shifting them and recognizing special properties of results in pointers (other kind of registers). So it may be that other pointers are set when working with primes (i do not remeber exactly but 17 years ago i programmed Assembler on a Motorola 6510 microprocessor and it was like that. It may be the same with Motorolas' still. Does anybody know this more exactly?)
If you see a 2.0 it does not have to be a 2.0, it may be a 2.0000000000000000000000001 for your machine instead. That should cause no trouble if you work with the result as of class real further on. If you need to convert the result to a value of class integer you will have to take care of the rare situation when uncoerxable values will be resulted:
set x to 61
set b to ((x + (2.6 - (2.6 mod 1))) - x)
--> Add lines like this:
return convertREALtoInteger(b)
on convertREALtoInteger(x)
try
set x to x as integer
on error
error (x as text) & " cannot be converted into an INTEGER because it was calculated from a prime number!"
end try
return x
end convertREALtoInteger
and you will have simulated exactly what your AppleScript system did but extended the error message.
So you may tell it a bug in AppleScript. But it is not, the programming language cannot take care of informatically wrong issues. AppleScript is tolerant, i know, but not all the time... ;-)
THIS script detects prime numbers by using a class identification:
-- 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!
-- Open the event log before running the script!
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 > 9 then -- Numbers below one are always primes!
repeat with z from 2 to 9 -- If a number can be divided by another number it's not a prime!
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
end repeat
else
set isPrime to true -- The number was between 0 and 9 and is a prime!
end if
return isPrime
end isItaPrime
Surprised? That's computers.
Regards,
Thomas
_______________________________________
Thomas Johannes Matthias Kuehner
Developer
AppleScripter
Administrator
Webmaster
macgix - software,network,consulting
http://www.macgix.com
http://www.ScriptMyMac.de