Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gives you the highest power of x less then provided number n
var highestPower = function(n, x) {
var res = 0;
for (var i = n; i >= 1; i--){
var pow = Math.pow(x, i);
if (pow < n) { res = pow; break;}
}
return res;
}
//Log solution
var highestPowerLog = function(n, base) {
var pow = 0;
if (base == 2) {
var pow = parseInt(Math.log2(n))
} else {
var pow = parseInt(Math.log(n))
}
return Math.pow(base, pow);
}
highestPower(10, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.