Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find if a Positive Integer is power of 2 , 3 , 4
<?php
function is_Power_of_four($n)
{
$x = $n;
while ($x % 4 == 0) {
$x /= 4;
}
if($x == 1)
{
return "$n is power of 4";
}
else
{
return "$n is not power of 4";
}
}
print_r(is_Power_of_four(4)."\n");
print_r(is_Power_of_four(36)."\n");
print_r(is_Power_of_four(16)."\n");
?>
<?php
function is_Power_of_three($n)
{
$x = $n;
while ($x % 3 == 0) {
$x /= 3;
}
if($x == 1)
{
return "$n is power of 3";
}
else
{
return "$n is not power of 3";
}
}
print_r(is_Power_of_three(9)."\n");
print_r(is_Power_of_three(81)."\n");
print_r(is_Power_of_three(21)."\n");
?>
<?php
function is_Power_of_two($n)
{
if(($n & ($n - 1)) == 0)
{
return "$n is power of 2";
}
else
{
return "$n is not power of 2";
}
}
print_r(is_Power_of_two(4)."\n");
print_r(is_Power_of_two(36)."\n");
print_r(is_Power_of_two(16)."\n");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.