Permutations - and BIG numbers

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Permutations - and BIG numbers

Post by bjb007 » Thu Jul 10, 2008 7:53 pm

As a mental challenge I wrote a prog
to calculate permutations i.e. the number
of possible sets of unique numbers in a
larger set of numbers.

The formula involves factorial calculations
i.e. factorial 10 is 10 x 9 x 8 x 7 ..... x 2 x 1.

The numbers get BIG as you can imagine.
Try factorial 99 (99!) on your calculator
and see how far you get.

Rev hits the limit at around 200 so instead of
using an "int" one would normally go for a "long".
But (and I don't regard this as a deficiency) Rev
doesn't seem to have one or a way to use
"scientific" notation.

This was a problem way back (before Win, perhaps
before you were born) when "micro-computers" were
limited to 640k memory less what the OS needed.

One of the techniques I recall was called "scaling" and
involved dividing the numbers by a factor which moved
the decimal point to the left so that the available slots
to the right of the decimal point were used.

The trick was deciding the divisor so as to not cause
an overflow and loss of accuracy.

Does anyone have any info. on the "capacity" of Rev
when dealing with large numbers and/or scaling?

Had a quick look in the Rev help without any luck.
Life is just a bowl of cherries.

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post by malte » Thu Jul 10, 2008 8:31 pm

Maybe the format function might help you.

From the docs:

put format("%1.3e",865.3) -- returns 8.653e+02, scientific notation

on mouseUp pMouseBtnNo
local tValue
put 1 into tValue
repeat with i=1 to 99
put i*tValue into tValue
end repeat
put format("%1.3e",tValue)&cr&tValue
end mouseUp

Hope that helps,

Malte

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Permutations - and BIG numbers

Post by bjb007 » Thu Jul 10, 2008 11:42 pm

Malte

This line

put i*tValue into tValue

give a "range overflow" error
as before with big values (c.200)
since the number is still an int
at this stage.

Did think of using the "format"
when the numbers are input
but at small values the scientific
notation will produce inaccurate
results.

Can't recall if a big number in
scientific notation can be multiplied
by an int. or not. Will give it a try
when time allows.

Have to look for info and try the
"scaling" trick.
Life is just a bowl of cherries.

Post Reply