Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
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 answers
Answer question

0

How about

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

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!