A response to Craig Smith’s Math Article…
"I am not a mathematician by any means, so I have very little idea about why this is called mod, or even what it is really useful for, but I think that it at least has a coolness factor that deserves discussion. "
“Modular arithmetic (sometimes called modulo arithmetic, or clock arithmetic because of its use in the 24-hour clock system) is a system of arithmetic for integers, where numbers “wrap around” after they reach a certain value ” the modulus. Modular arithmetic was introduced by Carl Friedrich Gauss in his book Disquisitiones Arithmeticae, published in 1801.” from http://en.wikipedia.org/wiki/Modular_arithmetic
“And although interesting, I still can’t think of a real world application for this.”
set r to {}
set desiredRepeatValue to 9 -- This is the highest value I want to use
repeat with a from 1 to 30 -- But I have 30 things to iterate through...
set r to r & (a mod (desiredRepeatValue + 1)) -- So I mod my incrementer by my highest value + 1.
-- Without the "+1" you won't get the high value because any clean division of a number equals zero.
-- Poorly worded maybe... In this case, clean means "a factor of" like 30 is a factor of 10. Hey I'm no mathmagician either!
end repeat
-- returns: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8}
I often use mod for times when I need a series of repeating values. Say I need to make 20 picture boxes, each with an Applescript tag of “Image_1” (“Image_2”,etc.), on a each page but I have 10 pages to do this on. I know every page will require me to “reset” my variables so the boxes fall in the same place on each page. This allows me to iterate through the 10 pages with the mod function “resetting” the variable at each page interface.
Very simplified example…
set r to {}
set numberOfPages to 10
set highestTagValue to 20
repeat with a from 1 to (numberOfPages * highestTagValue)
set r to r & (a mod (highestTagValue + 1))
end repeat
Hopefully that wasn’t too horrible of an explanation. It’s not a function you necessarily use everyday but there are some clever uses some of you may not have thought of.
Jim Neumann
BLUEFROG