I'm seeking an isPrime(rangeA, rangeB) AppleScript
Excuse me if this post isn't exactly appropriate for the audience.
I want to initially graph variances, whether there are more or less primes per thousand numbers, say, starting from zero to ten thousand, then comparing these and any subsequent set of numbers to the range of from one million one thousand to one million two thousand, or to the limit of the script's range ... to hundreds of billions.
Imagination and a good command of statistical functions, and one can realize how interesting such a script is and can be. I generated a Euler 2.17 curve with/from variances of the thirty to sixty primes I was generating per thousand, doing this on a TI-92 Plus ... ; the TI-92 had a quick and simple Yes/No=isPrime() function.
Many moons ago I was doing this kind of fiddling around with primes, I stopped, and today I want to pick up where I left off. I found an excellent script here, described below:
Subject: Sieve of Erastothenes for Primes (Just for the fun of it)
Written by hayne on 2006-10-27
Message
========================================================================
I came upon this thread when googling for an implementation of the "Sieve" in AppleScript.
One thing I immediately noticed is that the script supplied above uses the 'sqrt' function which is apparently not a standard part of AppleScript but is (I have read) part of an osax supplied by Satimage.
I modified the Sieve script to avoid the use of 'sqrt' by instead comparing the square of Factor with n.
Here's the modified script:
Applescript:
------------
-- This script is based on a script from: http://bbs.applescript.net/viewtopic.php?pid=49600
-- I modified it to avoid the need for 'sqrt' by comparing FactorSq with n
-- Cameron Hayne (macdev@hayne.net) Oct 2006
script PL
-- storing the list we will build as a property of an enbedded script
-- speeds up the list manipulations required considerably.
property P_List : missing value -- will be the list of primes
end script
-- Reset the list so it won't retain earlier results. A property is only reset when a script is compiled unless explicitly reset.
set PL's P_List to {}
set howBig to display dialog "Please Enter the Upper Bound on the List" buttons {"Bail Out", "Enter"} default button "Enter" default answer "Please enter an integer here; it is not checked"
if button returned of howBig is "Bail Out" then return
set n to (text returned of howBig) as integer
-- Fill the list to be pruned. We can get its 'length', but we know that's (n + 1) div 2.
fill_List(n)
set list_length to (n + 1) div 2
-- Assign 'missing value' to a variable for speed in the repeat.
set zapped to missing value
-- Remove multiples of primes from the list
repeat with i from 2 to list_length
set Factor to item i of PL's P_List
if (Factor is not zapped) then
set FactorSq to (Factor * Factor)
if (FactorSq > n) then
exit repeat
else
-- Zap multiples of Factor, starting with Factor squared. (Lower multiples already done.)
-- This repeat won't be excuted if ((FactorSq+ 1) div 2) > list_length.
repeat with j from ((FactorSq + 1) div 2) to list_length by Factor
set item j of PL's P_List to zapped
end repeat
end if
end if
end repeat
return PL's P_List's numbers
-- Handlers
to fill_List(num)
repeat with j from 1 to num by 2
set end of PL's P_List to j
end repeat
set item 1 of PL's P_List to 2
end fill_List
------------
========================================================================
I don't know enough AppleScript to modify it to give me a range. Seems to me the zapped variable needs to be implemented in order for the script to function, and altering the script for a range defeats the efficiency of the script.
I realize this is not a request from the President of the United States, though, any one have any functioning isPrime scripts, or the time to fiddle with the above script to bring it to function a range?
A hearty hooray! to anyone so inclined to be so kind!


