Comprueba si el número dado es una potencia de d donde d es una potencia de 2

Dado un entero n, encuentre si es una potencia de d o no, donde d es en sí misma una potencia de 2.
Ejemplos: 

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

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

Método 1 Tome el registro del número dado en base d, y si obtenemos un número entero, entonces el número es la potencia de d.

C++

// CPP program to find if a number is power
// of d where d is power of 2.
#include<bits/stdc++.h>
 
 
bool isPowerOfd(int n, int d)
{
  //Take log of the given number on base d,
  //and if we get an integer then number is power of d.
   
    return ((int)(log(n) /log(d)) == (int)log(n) / (int)log(d));
 
}
 
/* 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;
}

Python3

#Python3 program to find if a number is power
# of d where d is power of 2.
 
from math import log
 
def isPowerOfd(n, d):
 
  #Take log of the given number on base d,
  #and if we get an integer then number is power of d.
  return log(n) / log(d) == log(n) // log(d)
 
 
# Driver program to test above function*
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 phasing17

Javascript

// JavaScript program to find if a number is power
// of d where d is power of 2.
 
function isPowerOfd(n, d)
{
  //Take log of the given number on base d,
  //and if we get an integer then number is power of d.
   
    return (Math.floor((Math.log(n) / Math.log(d)))) == (Math.log(n) /Math.log(d));
 
}
 
/* Driver program to test above function*/
let n = 64, d = 8;
if (isPowerOfd(n, d))
    console.log(n + " is a power of " + d);
else
    console.log(n + " is not a power of " + d);
 
 
//This code is contributed by phasing17
Producción

64 is a power of 8

Método 2 Siga dividiendo el número por d, es decir, haga n = n/d iterativamente. En cualquier iteración, si n%d se vuelve distinto de cero y n no es 1, entonces n no es una potencia de d, de lo contrario, n es una potencia de d.
Método 3 (bit a bit) 
Un número n es una potencia de d si se cumplen las siguientes condiciones. 
a) Solo hay un bit establecido en la representación binaria de n (Nota: d es una potencia de 2) 
b) El recuento de bits cero antes del (único) bit establecido es un múltiplo de log 2 (d).
Por ejemplo: para n = 16 (10000) y d = 4, 16 es una potencia de 4 porque solo hay un bit establecido y un conteo de 0 antes de que el bit establecido sea 4, que es un múltiplo de 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>
Producción

64 is a power of 8

Complejidad del tiempo: O(log 2 n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por aganjali10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *