Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

259
Vistas
Check an number in an array is a powers number

How to check which number is a power in this array? arr = [16, 32, 72, 96]

output: [16, 32] because 16 = 4^2 and 32 = 2^5

It's not a solution about power of 2, it's about power of n.

This is my actual code :

let array1 = [16, 32, 72];
function findPowofNum(array1) {
    // var result = [];
    if (array1.length == 1)
        return true;
    for(let i = 2; i * i <= array1.length; i++) {
       let value = Math.log(array1/length) / Math.log(i);
       if((value - Math.floor(value)) < 0.00000001)
          return true;
    }
    return false;
}
console.log(findPowofNum(array1));

Can you give me a example for this solution by javascript?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

How about

arr = [16, 32, 72, 96].filter(
    value => Number.isInteger(Math.log2(value))
)
about 4 years ago · Juan Pablo Isaza Denunciar

0

const numbers = [16, 32, 72, 96];
const power = numbers.filter(isPowerOfTwo);
function isPowerOfTwo(n)
{
  if (n == 0)
    return 0;
  while (n != 1)
  {
    if (n%2 != 0)
      return 0;
    n = n/2;
  }
  return 1;
}

console.log(power);

To know more about this visit visit: https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use a custom boolean function to iterate through the elements and append to an empty array as you check like this..

function isPowerofTwo(n){
       return (Math.ceil((Math.log(n) / Math.log(2))))
            == (Math.floor(((Math.log(n) / Math.log(2)))));
}
let arr= [16, 32, 72, 96];
let values=[]
for(let i=0;i<arr.length;i++){
 if(isPowerofTwo(arr[i])){
   values.push(arr[i]);
   }
}
console.log(values);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda