Two methods to find if a number is a power of any number n


  • 0
    M
    1) Convert to base n and check the pattern "10*" appears in that base representation
    
    eg 4 base 4 = 10
       16 base 4 = 100
       3 base 3 = 10 
       9 base 3 = 100
    
    Integer.toString(num,n).matches("10*")
    
    2) (Math.log10(num)/Math.log10(n))%1==0
    
    eg Math.log10(64)/Math.log10(4) = 3.0
       3.0 % 1 = 0
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.