asmith Posted November 29, 2007 Share Posted November 29, 2007 do we got a function to check that whether a number is " 2 base " or not ? sorry i don't know what it can be in english : 2 base : 2 4 8 16 32 .... 3 base : 3 9 27 81 ... or i should code it individuali ? Quote Link to comment https://forums.phpfreaks.com/topic/79420-solved-2-4-8-16/ Share on other sites More sharing options...
Syco54645 Posted November 29, 2007 Share Posted November 29, 2007 There is base_convert, but it cannot check a number's base, merely convert it. Not sure if that is what you meant. http://php.net/base_convert Quote Link to comment https://forums.phpfreaks.com/topic/79420-solved-2-4-8-16/#findComment-402075 Share on other sites More sharing options...
obsidian Posted November 29, 2007 Share Posted November 29, 2007 There is base_convert, but it cannot check a number's base, merely convert it. Not sure if that is what you meant. http://php.net/base_convert If I'm not mistaken, the description of the issue seems to be covering roots, not truly bases. If it were base 2, it would be binary and only contain 0s and 1s. So, correct me if I'm wrong, but you are wanting to find out whether or not a number has a root of X, correct? If this is the case, I'm not sure there is a built in function to do it, but a function like this may help: <?php /** * Checks whether number $n is a power of $root * @param int $n * @param int $root * @return array */ function is_of_root($n, $root) { $pow = 0; $x = 0; while ($x < $n) { ++$pow; $x = pow($root, $pow); } if ($n == $x) { return array(TRUE, $pow); } else { return array(FALSE, -1); } } header("Content-type: text/plain"); print_r(is_of_root(27, 3)); print_r(is_of_root(100, 10)); print_r(is_of_root(134217728, ); ?> As you can see, the first key of the array returned is a boolean TRUE or FALSE as to whether the provided number has a root of the $root provided, and the second element of the array is the magnitude that creates your number in question. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/79420-solved-2-4-8-16/#findComment-402080 Share on other sites More sharing options...
asmith Posted November 29, 2007 Author Share Posted November 29, 2007 more like it, see this : if (x is "2 base" ) echo "valid" else (echo invalid) if i set x= 30 , the output is invalid , if the x=64 , the outpu tis valid 8 valid 16 valid 20 invalid 24 invalid 32 valid 40 invalid 128 valid 150 invalid 256 vald 512 valid Quote Link to comment https://forums.phpfreaks.com/topic/79420-solved-2-4-8-16/#findComment-402086 Share on other sites More sharing options...
obsidian Posted November 29, 2007 Share Posted November 29, 2007 I just updated my previous post with a function that may help... see above. Quote Link to comment https://forums.phpfreaks.com/topic/79420-solved-2-4-8-16/#findComment-402096 Share on other sites More sharing options...
asmith Posted November 29, 2007 Author Share Posted November 29, 2007 yea it works, but wanted to know maybe theres's a function for it thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/79420-solved-2-4-8-16/#findComment-402101 Share on other sites More sharing options...
obsidian Posted November 29, 2007 Share Posted November 29, 2007 yea it works, but wanted to know maybe theres's a function for it thanks ! I just browsed the math section of the PHP manual, and I can't find anything that is a reverse lookup like that. That is why I wrote up the function above quickly. Quote Link to comment https://forums.phpfreaks.com/topic/79420-solved-2-4-8-16/#findComment-402105 Share on other sites More sharing options...
asmith Posted November 29, 2007 Author Share Posted November 29, 2007 I just browsed the math section of the PHP manual, and I can't find anything that is a reverse lookup like that. That is why I wrote up the function above quickly. thanks a bunch , you really helped! Quote Link to comment https://forums.phpfreaks.com/topic/79420-solved-2-4-8-16/#findComment-402118 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.