[ANN] New version of the MasterLibrary is available
Geoff Canyon
gcanyon at gmail.com
Sun Feb 1 15:48:51 EST 2015
I couldn't resist. __nCombFactors is slower than __nCombFloor, although
faster than the original function (slightly, in my testing), but it offers
the advantage that any result that is within the number limits of LC will
be returned correctly, regardless of how large the intermediate values
would be if they were calculated. For example, __nCombFactors is the only
one that (I think) returns the correct value for 80 c 40
The next step (obviously) would be to add a bignum * function, so that
nearly any combination could be calculated.
function __factorialFloor x,f
if x <= f then return 1
return x * __factorialFloor(x-1,f)
end __factorialFloor
function __nCombFloor n, r
if (r > 0) or (r <= n) then \
return __factorialFloor(n,max(r,n-r))/__factorial(min(r,n-r))
Answer "nComb error:" && r && "is not between 0 and" && n
exit to top
end __nCombFloor
function __nCombFactors n, r
if (r <= 0) or (r > n) then
Answer "nComb error:" && r && "is not between 0 and" && n
exit to top
end if
put __factorialFactors(n,max(r,n-r)+1) into tNum
put __factorialFactors(min(r,n-r)) into tDen
put __removeItems(tNum,tDen) into c
replace comma with "*" in c
if c is empty then return 1 else return value(c)
end __nCombFactors
function __removeItems N,D
repeat until D is empty
if item 1 of D > item 1 of N then
put item 1 of N & comma after R
delete item 1 of N
else
delete item 1 of N
delete item 1 of D
end if
end repeat
if N is empty then return char 1 to -2 of R else return R & N
end __removeItems
function __factorialFactors x,f
if f is empty then put 2 into f
repeat with i = f to x
put __primeFactors(i),"" after R
end repeat
sort items of R numeric
return char 2 to -1 of R
end __factorialFactors
function __primeFactors x
repeat with i = 2 to x div 2
repeat until x mod i <> 0
put i,"" after R
put x div i into x
if x = 1 then return char 1 to -2 of R
end repeat
end repeat
return x
end __primeFactors
On Sun, Feb 1, 2015 at 2:08 PM, Geoff Canyon <gcanyon at gmail.com> wrote:
> You could handle (some) larger combinatorials (and speed up the code) by
> modifying like so:
>
> function __factorialFloor x,f
> if x <= f then return 1
> return x * __factorialFloor(x-1,f)
> end __factorialFloor
>
> function __nCombFloor n, r
> if (r > 0) or (r <= n) then \
> return __factorialFloor(n,max(r,n-r))/__factorial(min(r,n-r))
> Answer "nComb error:" && r && "is not between 0 and" && n
> exit to top
> end __nCombFloor
>
> Of course, to handle even more/larger possible values you could decompose
> the arguments to their prime factors and eliminate dupes rather than
> divide, but this was a fairly easy optimization.
>
> On Sun, Feb 1, 2015 at 12:02 PM, Michael Doub <mikedoub at gmail.com> wrote:
>
>> Release 24
>> * added:
>> __Zap - a function that returns only POSIX printable characters,
>> space, tab and return from a
>> string
>> __GetRegexMatches - Finds all the matches of pregex in ptext and
>> returns a list of the
>> start,end char positions
>> __deleteExtraTokens - This fuction witll remove sequences of the
>> specified token that are greater than
>> one.
>> __combinatorial - returns all possible subsets of a set, where n = #
>> of objects in the set
>> __nComb - Returns the # of combinations of n objects taken r at a time
>> __factorial - Returns x!
>> __latLongDist - Finds distances between two points based on Latitude
>> and Longitude
>> __dd2dms - Converts a digital longitude or Latitude into degrees,
>> minutes, seconds, direction format.
>> __dms2dd - Converts a degrees, minutes, seconds, direction longitude
>> or latitude to digital format
>>
>> https://www.dropbox.com/s/3wpwn3hfbmpl7sk/MasterLibrary.livecode?dl=0
>>
>> Enjoy...
>>
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>
>
More information about the use-livecode
mailing list