Click to See Complete Forum and Search --> : maths stuff


David Harrison
03-05-2003, 02:36 PM
in maths specifically in statistics there's something called combinations and permutations, and I need a script that will calculate combinations, they work like this,

eg. lottery, 49 balls to choose from, 6 balls are chosen, therefore the number of combinations for the balls drawn is

49 choose 6 = (49!/(49-6)!)/6! = 13983816

( 49 factorial / (49-6) factorial ) / 6 factorial

well i didn't know how to do choose so i did it in terms of factorials but i didn't know how to do those either, so i had to write my own script:
eg. if n=4

a=n

while(a>1){a=a-1; n=n*a}

and after this is run n=24 which is 4!

so with three factorial to work out like this it's very cpu intensive especially when I need it to run this up to and sometimes more than 50 times in one go, I need it to calculate the binomial distribution, if you know what that is.

basically my question is, can someone tell me how to tell the comp. to workout combinations and failing that factorials please?

dabush
03-05-2003, 05:18 PM
sorry, but i remember learining permutations combinations in the eighth grade, but ive forgot about it since then. please give me the following info, and i'll write u a script...


1) How do you find the factorial of a number? Give the formula and an example.

2) How do you find the number of combinations? Give the formula and an example.

dabush
03-05-2003, 05:38 PM
never mind. i found my old textbook. will have script in few mins!

dabush
03-05-2003, 06:11 PM
ok i got it. i tried ur example with 49 C 6 and it came out just how u said it would be.

this script will check the following:
1) neither number is null
2) neither number contains invalid characters
3) second number is not greater than first

If the script finds that one of these mistakes are made, it will alert you that you made a mistake. If there is no mistake, it will write the answer in the box. You can either do Permutations OR Combinations. Check it out...it works and its cool!

The script...HERE (http://www.angelfire.com/scary/scripts/permComb.html)

dabush
03-05-2003, 09:37 PM
lavalamp, u see this?

David Harrison
03-06-2003, 03:52 PM
Yeah, i downloaded your script but i've already got a script that works out factorials the long way round, i need a quick way such as Math.factorial(4) except not that because that doesn't work. you know what i mean???

boojum
03-06-2003, 04:23 PM
javascript doesnt support modules (at least it doesnt client side) so you'll have to program the function yourself if you want something handy like Factorial(). you also cant alter the DOM so you wont be able to get object.factorial() functionality. but even so, it will be the long way to calculate no matter who writes it and how you access it. factorials take alot of computation, although there are better and worse ways to do it on a given rig. these are the names of the functions for the Math object:
abs Method | acos Method | asin Method | atan Method | atan2 Method | ceil Method | cos Method | exp Method | floor Method | log Method | max Method | min Method | pow Method | random Method | round Method | sin Method | sqrt Method | tan Method

i dont think any will help you with factorials. but i could be completely wrong about everything i said too.

dabush
03-09-2003, 11:28 PM
Originally posted by lavalamp
Yeah, i downloaded your script but i've already got a script that works out factorials the long way round, i need a quick way such as Math.factorial(4) except not that because that doesn't work. you know what i mean???

Using what I gave you, there is a quick way...

getFactorial(4)