Re: math help

From: Date: Fri, 25 Aug 2000 20:41:33 +0000
Subject: Re: math help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-13725@lists.php.net to get a copy of this message
Quoting Hans H. Anderson (hans@hansanderson.com): > Hi All, > > I'm not a great mathematician. Can someone give me an idea of how to > calculate total possibilities? > > For instance, given 1,2,3,4,5, how do I determine how many possible ways > it will be to order them? 2,1,4,5,3, 3,2,4,5,1, etc. Assuming that there cannot be duplication (ie, 3,3,3,3,3 is not valid) then you want factorial, expressed in common math as 5! For example, this might be five different people waiting for something. Each number will only appear once in the series. The math is 5 * 4 * 3 * 2 * 1, but you can always leave off the * 1. There may be a function to calculate it, but if not it's quite simple: ---- $factorial = 5; $sum = $factorial; for($i = ($factorial-1); $i > 1; $i--) { $sum = $sum * $i; } echo $sum; ---- If it is possible to have 3,3,3,3,3, for example possible lock combinations, then the formula is even easier, take the number of possible choices in any given column raised to the power of the number of columns. For example, in our case say there could be 0-9, or 0,0,0,0,0 - 9,9,9,9,9, then the answer is 10^5, or 100000 possibilities (0-99999) -- this works since we have 0-9 for our range. This is commonly used for binary, for example if you have 0-1 for the range and eight columns, it's 2^8, or 256, a byte. ---- $columns = 5; $choices = 10; $sum = $choices ^ $columns; echo $sum; ---- Hope this helps. aaron.

« previous php.general (#13725) next »