Due to Bruce Phillips help I could solve my prime number generating script problems up to 90%. The last that is missing is to adapt the “Sieve of Eratosthenes”-algorithm. The script by now looks like this. Open the Event Log window and have a look:
-- Different ways to create prime numbers
on run
-- Brute Force Methode 1
repeat with n from 1 to 100
set composite to 0
repeat with i from 2 to (n - 1)
if (n mod i = 0) then set composite to 1
end repeat
if composite = 0 then log n
end repeat
-- Brute Force Methode 2
repeat with n from 1 to 100
set composite to 0
set sqr to intsqrt(n)
repeat with i from 2 to sqr
if (n mod i = 0) then set composite to 1
end repeat
if (composite is 0) then log n
end repeat
-- Primes method originally using the Math Commands osax. Written by ???
local min, max, neverZero, maxDiv, divTry, res
-- Here you can set any range that suits you
set {min, max} to {1, 200}
set res to {}
-- By some people's opinion, 1 is not considered a prime
if min = 1 then set min to 2
-- 2 is special, so it has to be handled in its own way
if min = 2 then set {res, min} to {{2}, 3}
-- We can't use even numbers, because they are never primes, except for 2
if min mod 2 = 0 then set min to min + 1
repeat until min > max
set maxDiv to intsqrt(min)
set {neverZero, divTry} to {true, 3}
repeat until divTry > maxDiv
if min mod divTry = 0 then
set neverZero to false
exit repeat
end if
set divTry to divTry + 2
end repeat
if neverZero then
set res to res & min
log min
end if
set min to min + 2
end repeat
(*
-- The sieve of Eratosthenes
const N = 10000
var canceled: array [2..N] of boolean
{ Initialising the field of primes }
{ All of the number are not part of canceled on beginning }
for i = 2 to N do
canceled[i] = false
end
i = 2
while i*i <= N do
if not canceled[i] then
{ i is a prime, cancel its multiples beginning with i*i }
for j = i*i to N by i do
canceled[j] = true
end
i = i+1
end
for i = 2 to N do
if not canceled[i] then
print i; ", ";
end
*)
end run
on intsqrt(n)
return (do shell script "echo 'sqrt (" & n & " )' | /usr/bin/bc")
end intsqrt