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

给定一个整数n,找出它是否是d的幂,其中d本身是2的幂。 例如:

null
Input : n = 256, d = 16Output : YesInput : n = 32, d = 16Output : 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的幂,因为只设置了一个位,并且在设置的位为4之前计数为0,这是log的倍数 2. (4).

C++

// CPP program to find if a number is power
// of d where d is power of 2.
#include<stdio.h>
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

<?php
// PHP program to find if a number
// is power of d where d is power of 2.
function Log2n( $n )
{
return ( $n > 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

<script>
// Javascript program to find if
// a number is power of d
// where d is power of 2
function Log2n(n)
{
return (n > 1) ? 1 +
Log2n(n / 2) : 0;
}
// Function to count the number
// of ways to paint  N * 3 grid
// based on given conditions
function isPowerOfd(n, d)
{
var 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
var n = 64, d = 8;
if (isPowerOfd(n, d))
document.write(n +
" is a power of " + d);
else
document.write(n +
" is not a power of " + d);
// This code is contributed by Khushboogoyal499
</script>


输出:

64 is a power of 8

时间复杂性: O(原木) 2. n)

辅助空间: O(1)

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享