Re: The Pyramid - a challenge...
Re: The Pyramid - a challenge...
- Subject: Re: The Pyramid - a challenge...
- From: Martin Orpen <email@hidden>
- Date: Wed, 27 Apr 2005 21:04:41 +0100
on 25/4/05 00:59, Nigel Garvey at email@hidden wrote:
[snipped v. helpful script]
> The result looks very pretty in a browser though. :-)
Thanks for posting the script Nigel - makes mine look embarrassingly
disorganized... Wonder if scripts are like their owners? :)
My first foray into html made me think about another bit of number theory
that works better as a picture. My usual disclaimers apply for the
crappiness of the code - especially my inability to find a simple method of
creating a *square spiral*:
-- html stuff
property theHeader : "<html>
<head>
<title>Ulam's prime diagonal</title>
</head>
<body>
<table width=\"480\" border=\"0\">
<tr>"
property theFooter : "</tr>
</table>
</body>
</html>"
property tCell1 : "<td width=\"30\">
<div align=\"center\"><font size=\"2\">"
property tCell2 : "</font></div></td>"
property tDivider : "</tr>
<tr>"
-- messy attempt at a spiral...
set s to {}
repeat 17 times
set end of s to {}
end repeat
set sP to 9
set n to 17
set end of item sP of s to n
set x to 8
set y to 0
repeat x times
repeat y + 1 times
set n to n + 1
set end of item sP of s to n
end repeat
repeat y + 1 times
set n to n + 1
set sP to sP - 1
set end of item sP of s to n
end repeat
repeat y + 2 times
set n to n + 1
set beginning of item sP of s to n
end repeat
repeat y + 2 times
set n to n + 1
set sP to sP + 1
set beginning of item sP of s to n
end repeat
set y to y + 2
end repeat
-- create the html
set h to ""
set h to h & theHeader
repeat with foo from 1 to count of s
set bar to count of item foo of s
set woo to item foo of s
repeat with z from 1 to count of woo
set nC to item z of woo
if my primeCheck(nC) is false then
set h to h & tCell1
set h to h & nC
set h to h & tCell2
else
set h to h & tCell1
set h to h & "<b>"
set h to h & nC
set h to h & "</b>"
set h to h & tCell2
end if
end repeat
set h to h & tDivider
end repeat
set h to h & theFooter
-- write the html to OS X desktop
writeFile(h, "ulamgrid.html")
-- an ancient prime checker of mine
on primeCheck(myInteger)
if myInteger mod 2 is 0 then
return false
else
set myInteger to myInteger as string
set x to 2
set resultList to {}
-- scaled down version of my 9x test
repeat with x from 1 to 5
set end of resultList to do shell script "echo 'scale=1; ((" & x
& "^" & myInteger & ") -" & x & ")/" & myInteger & "' | bc"
end repeat
set myCheck to {}
repeat with i in resultList
set end of myCheck to last item of i
end repeat
if myCheck is {"0", "0", "0", "0", "0"} then
return true
else
return false
end if
end if
end primeCheck
on writeFile(fD, fN)
set t to path to desktop
set posixT to POSIX path of t
set f to open for access file ((t as string) & fN) with write permission
write fD to f
close access f
end writeFile
Which should hopefully build a table showing that primes love diagonals.
A fitting task for a boring train journey as the discovery was originally
made by Stanislaw Ulam while he doodled during a boring lecture.
--
Martin Orpen
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden