Re: n! script
Re: n! script
- Subject: Re: n! script
- From: Martin Crisp <email@hidden>
- Date: Sun, 14 Nov 2004 15:27:07 +1100
- Organization: Tesseract Computing
On Sat, 13 Nov 2004 05:24:49 +1100, Brian Johnson wrote
(in message
<email@hidden>):
>
> 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...
100! =
73964627576010540934496437274759430449075257942548698041089118602203
93463809022870062031778140972081943760435542662019792862595843686400
0000000000000000000000
I'm simply presuming that's correct... (works for smaller numbers)
> - 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)
[snip]
This is faster, and shorter. I haven't spent any time trying to
improve speed... Maybe not clearer:
on run
set oldNumStr to "3"
set numSet to false
repeat until numSet
set theNumStr to text returned of (display dialog ¬
"Find factorial of what *non-negative integer*?" ¬
default answer oldNumStr with icon note)
try
set theNumStr to (theNumStr as integer) as string
set numSet to true
end try
end repeat
set factStr to factorialStr(theNumStr)
display dialog ({theNumStr, "! = ", factStr} as string) with icon
note
end run
on factorialStr(aNumberStr)
if aNumberStr = "1" or aNumberStr = "0" then
return "1"
else
return(strMult(factorialStr((aNumberStr - 1) as string),
aNumberStr))
end if
end factorialStr
on strMult(numStr1, numStr2)
set inputCols to every character of numStr1
set numCols to length of inputCols
set inputRows to every character of numStr2
set numRows to length of inputRows
set solutionRow to {}
set maxNumSolnDigits to (numRows + numCols + 1)
repeat with i from 1 to maxNumSolnDigits
set end of solutionRow to "0"
end repeat
repeat with R from numRows to 1 by -1
repeat with C from numCols to 1 by -1
set outputCell to every character of ¬
(((item C of inputCols) * (item R of inputRows)) as string)
if length of outputCell = 1 then set beginning of outputCell to
"0"
set tmpRightSoln to every character of ¬
(((item (R + C + 1) of solutionRow) + ¬
(item 2 of outputCell)) as string)
if length of tmpRightSoln = 1 then set beginning of tmpRightSoln
to "0"
set tmpLeftSoln to every character of ¬
(((item (R + C) of solutionRow) + (item 1 of outputCell) + ¬
(item 1 of tmpRightSoln)) as string)
if length of tmpLeftSoln = 1 then set beginning of tmpLeftSoln
to "0"
set tmpCarrySoln to every character of ¬
(((item (R + C - 1) of solutionRow) + ¬
(item 1 of tmpLeftSoln)) as string)
set item (R + C + 1) of solutionRow to ¬
((item 2 of tmpRightSoln) as string)
set item (R + C) of solutionRow to ((item 2 of tmpLeftSoln) as
string)
set item (R + C - 1) of solutionRow to ¬
((last item of tmpCarrySoln) as string)
end repeat
end repeat
repeat while item 1 of solutionRow = "0"
set solutionRow to rest of solutionRow
end repeat
return (solutionRow as string)
end strMult
Have Fun
Martin
_______________________________________________
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
References: | |
| >n! script (From: Brian Johnson <email@hidden>) |