t_machine Posted August 6, 2007 Share Posted August 6, 2007 I am creating a tournament script and need to have total matches as a multiple of 2 (2, 4, 8, 16, 32... ). How can i check the number to see if it's ok. example $matchesNum = 38;//obviously this is not a multiple How would I check that number to make sure it was 32 or 64... Thanks for any help Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 6, 2007 Share Posted August 6, 2007 Try somethign like: <?php function multipleof2($num){ while($num > 2){ $num /= 2; } if($num == 2){ return true; }else{ return false; } } if(multipleof2(32)){ //multiple of two }else{ //not a multiple of two } ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 6, 2007 Share Posted August 6, 2007 This works... I tested it: <?php function multiple($number){ $max = 20000; for($i=2;$i<$max;$i=$i*2){ if($number == $i){ return TRUE; break; }else{ continue; } } } if(multiple(10)){ echo'This is a multiple of 2.'; }else{ echo'This is NOT a multiple of 2.'; } ?> Quote Link to comment Share on other sites More sharing options...
chiprivers Posted August 6, 2007 Share Posted August 6, 2007 you could just use: if ($x % 2 == 0) { TRUE } Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 6, 2007 Share Posted August 6, 2007 that is right chiprivers, so if you were to put it into function form, it would look like this: <?php function mul($x){ if ($x % 2 == 0) { return TRUE; }else{ return FALSE; } } if(mul(38)){ echo'This is a multiple of 2.'; }else{ echo'This is NOT a multiple of 2.'; } ?> Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 6, 2007 Share Posted August 6, 2007 Folks, go back and read the real question. Even though the OP is asking about a multiple of 2 (for which a simple modulus test will suffice), s/he's really wanting to know if a number is a positive integer power of 2. 2,4,8,16,32 ..... i.e. 2^1, 2^2, 2^3, etc. http://ca.php.net/manual/en/function.log.php with base=2 might help Quote Link to comment Share on other sites More sharing options...
t_machine Posted August 6, 2007 Author Share Posted August 6, 2007 Thanks everyone for the replies AndyB does have the right idea but I am not sure how to implement it. Using that code, how would I know if 32 is a power of 2? I guess for simplicity I could just add those numbers in an array(2, 4, 6.....) then check if total matches in the array. I would prefer if a function could be created instead to check if the total matches, is a power of 2. Thanks Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 6, 2007 Share Posted August 6, 2007 <?php function multiple($number){ $max = 20000; $m = 1; for($i=2;$i<$max;$i=$i*2){ $m++; if($number == $i){ GLOBAL $ar; $m--; $ar = array(TRUE,$m); return $ar; break; }else{ continue; } } } if(multiple(32)){ echo'This is a multiple of 2.<br>2^'.$ar['1']; }else{ echo'This is NOT a multiple of 2.'; } ?> Quote Link to comment Share on other sites More sharing options...
mohaakilla51 Posted August 6, 2007 Share Posted August 6, 2007 Try somethign like: <?php function multipleof2($num){ while($num > 2){ $num /= 2; } if($num == 2){ return true; }else{ return false; } } if(multipleof2(32)){ //multiple of two }else{ //not a multiple of two } ?> This code will do what you want, because it just keeps dividing the number by 2 until it is less than or equal to 2. then, if it is 2, it is obviously a power of 2, or if it is something else, then it is not a power of 2. Example: 32/2= 16/2=8/2=4/2=2, therefore 32 is power of 2. 38/2= 19, already not a power of 2 Quote Link to comment Share on other sites More sharing options...
hasanatkazmi Posted August 6, 2007 Share Posted August 6, 2007 its not so complicated function isit($num) { $max=2000/*this will set ur upper limit, iterative will be better than recurrsive because it can set limit to the resources/time which server will utilize*/ for ($d=0;$d<$max;$d++) { if(2^$d==$num)return true; } return false; } isit($number) { //if it is }else { //if itisnt } Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 6, 2007 Share Posted August 6, 2007 I tried using is_int, but it was not working as expected with the log() function, so I used a comparison with round instead: <?php function multiple($number) { return (round(log($number, 2))==log($number, 2)); } ?> Quote Link to comment Share on other sites More sharing options...
t_machine Posted August 6, 2007 Author Share Posted August 6, 2007 Thank you all very much I have it working great now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.