Jump to content

[SOLVED] 2 4 8 16


asmith

Recommended Posts

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.