Skip to content

Instantly share code, notes, and snippets.

@ta-riq
Created August 26, 2017 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ta-riq/9f3cca450ce924b2319d1446223420b9 to your computer and use it in GitHub Desktop.
Save ta-riq/9f3cca450ce924b2319d1446223420b9 to your computer and use it in GitHub Desktop.
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