📜  检查给定数是否为d的幂,其中d为2的幂

📅  最后修改于: 2021-05-25 07:23:36             🧑  作者: Mango

给定整数n,请确定它是否是d的幂,其中d本身是2的幂。
例子:

Input : n = 256, d = 16
Output : Yes

Input : n = 32, d = 16
Output : No

方法1:以d为底的给定数字的对数,如果我们得到一个整数,则数字为d的幂。
方法2保持该数字除以d,即反复进行n = n / d。在任何迭代中,如果n%d变为非零且n不为1,则n不是d的幂,否则n是d的幂。
方法3(按位)
如果满足以下条件,则数字n是d的幂。
a)在n的二进制表示中只设置了一位(注:d是2的幂)
b)(仅)置位之前的零位计数是日志2 (d)的倍数。
例如:对于n = 16(10000)和d = 4,16是4的功率,因为只有一个比特组和0的计数之前设置位是4,其是log 2(4)的倍数。

C++
// CPP program to find if a number is power
// of d where d is power of 2.
#include
 
unsigned int Log2n(unsigned int n)
{
return (n > 1)? 1 + Log2n(n/2): 0;
}
 
bool isPowerOfd(unsigned int n, unsigned int d)
{
int count = 0;
 
/* Check if there is only one bit set in n*/
if (n && !(n&(n-1)) )
{
    /* count 0 bits before set bit */
    while (n > 1)
    {
    n >>= 1;
    count += 1;
    }    
 
    /* If count is a multiple of log2(d)
    then return true else false*/
    return (count%(Log2n(d)) == 0);
}
 
/* If there are more than 1 bit set
    then n is not a power of 4*/
return false;
}
 
/* Driver program to test above function*/
int main()
{
int n = 64, d = 8;
if (isPowerOfd(n, d))
    printf("%d is a power of %d", n, d);
else
    printf("%d is not a power of %d", n, d);
return 0;
}


Java
// Java program to find if
// a number is power of d
// where d is power of 2.
 
class GFG
{
static int Log2n(int n)
{
    return (n > 1)? 1 +
    Log2n(n / 2): 0;
}
 
static boolean isPowerOfd(int n,
                        int d)
{
int count = 0;
 
/* Check if there is
only one bit set in n*/
if (n > 0 && (n &
(n - 1)) == 0)
{
    /* count 0 bits
    before set bit */
    while (n > 1)
    {
        n >>= 1;
        count += 1;
    }
 
    /* If count is a multiple
    of log2(d) then return
    true else false*/
    return (count %
        (Log2n(d)) == 0);
}
 
/* If there are more
than 1 bit set then
n is not a power of 4*/
return false;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 64, d = 8;
    if (isPowerOfd(n, d))
        System.out.println(n +
                    " is a power of " + d);
    else
        System.out.println(n +
                    " is not a power of " + d);
}
}
 
// This code is contributed by mits


Python3
# Python3 program to find if a number
# is power of d where d is power of 2.
 
def Log2n(n):
    return (1 + Log2n(n / 2)) if (n > 1) else 0;
 
def isPowerOfd(n, d):
    count = 0;
     
    # Check if there is only
    # one bit set in n
     
    if (n and (n & (n - 1))==0):
        # count 0 bits
        # before set bit
        while (n > 1):
            n >>= 1;
            count += 1;
        # If count is a multiple of log2(d)
        # then return true else false
        return (count%(Log2n(d)) == 0);
 
    # If there are more than 1 bit set
    # then n is not a power of 4
    return False;
 
# Driver Code
n = 64;
d = 8;
if (isPowerOfd(n, d)):
    print(n,"is a power of",d);
else:
    print(n,"is not a power of",d);
 
# This code is contributed by mits


C#
// C# program to find if
// a number is power of d
// where d is power of 2.
using System;
 
class GFG
{
static int Log2n(int n)
{
    return (n > 1)? 1 +
    Log2n(n / 2): 0;
}
 
static bool isPowerOfd(int n,
                    int d)
{
int count = 0;
 
/* Check if there is
only one bit set in n*/
if (n > 0 && (n & (n - 1)) == 0)
{
    /* count 0 bits
    before set bit */
    while (n > 1)
    {
    n >>= 1;
    count += 1;
    }
 
    /* If count is a multiple
    of log2(d) then return
    true else false*/
    return (count % (Log2n(d)) == 0);
}
 
/* If there are more than
1 bit set then n is not
a power of 4*/
return false;
}
 
// Driver Code
static void Main()
{
int n = 64, d = 8;
if (isPowerOfd(n, d))
    Console.WriteLine("{0} is a " +
                    "power of {1}",
                            n, d);
else
    Console.WriteLine("{0} is not a"+
                    " power of {1}",
                            n, d);
}
 
// This code is contributed by mits
}


PHP
 1)? 1 +
    Log2n($n / 2): 0;
}
 
function isPowerOfd($n, $d)
{
    $count = 0;
 
// Check if there is only
// one bit set in n
if ($n && !($n & ($n - 1)))
{
     
    // count 0 bits
    // before set bit
    while ($n > 1)
    {
        $n >>= 1;
        $count += 1;
    }
 
    /* If count is a multiple of log2(d)
    then return true else false*/
    return ($count%(Log2n($d)) == 0);
}
 
    /* If there are more than 1 bit set
    then n is not a power of 4*/
    return false;
}
 
// Driver Code
$n = 64;
$d = 8;
if (isPowerOfd($n, $d))
    echo $n," ","is a power of ", $d;
else
    echo $n," ","is not a power of ", $d;
 
// This code is contributed by m_kit
?>


Javascript


输出:
64 is a power of 8

时间复杂度: O(log 2 n)

辅助空间: O(1)