n! script
n! script
- Subject: n! script
- From: Brian Johnson <email@hidden>
- Date: Fri, 12 Nov 2004 10:24:49 -0800 (PST)
OK, here's a working generalized version. Displaying really big results
get's beyond "display dialog" and 100! took so long I stopped it for fear
of run-away script, but it runs up through 40! ok so I think it's OK...
- brian johnson
(forgive my my short variable names and awkward constructions, I plead
'quick hack, in need of refinement' and throw myself on the mercy of the
court)
display dialog "n-Factorial Calculator. Please enter N " default answer 1
set n to (text returned of the result) as number
if n < 0 then
display dialog "Sorry. Undefined!"
else if n = 1 then
display dialog "1! = 1"
else
set p2 to {1}
repeat with i from 2 to n
set p1 to characters of (i as text)
set p2 to BigProd(p1, p2)
set p2 to trim(p2)
set AppleScript's text item delimiters to ""
--display dialog (i as text) & "! = " & (p2 as text)
end repeat
display dialog "Done! " & (i as text) & "! = " default answer (p2 as text)
end if
on BigProd(p1, p2)
set x1 to reverse of p1
set x2 to reverse of p2
set p to {0}
set j to 0
repeat with t in x2
--display dialog "t=" & t
set i to j
set r to {}
set pRow to {}
repeat j times
copy 0 to end of pRow
end repeat
set j to j + 1
repeat j times
copy 0 to end of r
end repeat
repeat with adigit in x1
set i to i + 1
set t1 to adigit * t + (item i of r)
set iplace to 1
set pvalue to 10 ^ iplace
set cdigit to t1 div pvalue
if cdigit = 0 then
copy cdigit to end of r
copy t1 to end of pRow
end if
repeat while cdigit â 0
set pdigit to t1 mod pvalue
copy cdigit to end of r
copy pdigit to end of pRow
set iplace to iplace + 1
set pvalue to 10 ^ iplace
set cdigit to t1 div pvalue
end repeat
end repeat
if (count of r) > (count of pRow) then copy last item of r to end of pRow
set carryover to 0
repeat with i from 1 to count of pRow
set t1 to item i of pRow
if i > (count of p) then copy 0 to end of p
set t2 to item i of p
set t3 to t1 + t2 + carryover
set carryover to t3 div 10
set pdigit to t3 mod 10
set item i of p to pdigit
end repeat
--display dialog "partial p {" & (p as text) & "}"
end repeat
return reverse of p
end BigProd
on trim(p2)
repeat with i from 1 to count of p2
if item i of p2 â 0 then return items i thru -1 of p2
end repeat
end trim
_______________________________________________
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