RE: [PHP] math help

From: Date: Fri, 25 Aug 2000 20:54:21 +0000
Subject: RE: [PHP] math help
Groups: php.general 
Request: Send a blank email to php-general+get-13728@lists.php.net to get a copy of this message
Instead of using a for loop or something of that nature you could always use recursion to really zip through this fast! :) Jay -----Original Message----- From: Aaron Abelard [mailto:aaron@aig4.com] Sent: Friday, August 25, 2000 3:42 PM To: php-general@lists.php.net Subject: Re: [PHP] math help 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#13728) next »