8000 GitHub - hgopi/power-of-a-number: Find the power of the given base to the exponent without using native methods like math.pow
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

hgopi/power-of-a-number

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

power-of-a-number

Find the power of the given base to the exponent without using native methods like math.pow

The Math.pow() function returns the base to the exponent power, that is, base<sup>exponent</sup>.

Create a function to replicate the behavior of that native method to find the exponent value of the given number.

function toPower(x, n) {
	if (n < 0) return toPower(1 / x, -n);
	else if (n === 0) return 1;
	else if (n === 1) return x;
	else if (n % 2 === 0) return toPower(x * x, n / 2);
	else if (n % 2 !== 0) return x * toPower(x * x, (n - 1) / 2);
}
console.log(toPower(2, 3)); // 8
console.log(toPower(10, 2)); // 100

Without recursion:

function toPowerWithoutRecursion(x, n) {
	if (n < 0) {
		x = 1 / x;
		n = -n;
	}

	if (n === 0) return 1;
	let y = 1;

	while (n > 1) {
		if (n % 2 === 0) {
			x = x * x;
			n = n / 2;
		} else {
			y = x * y;
			x = x * x;
			n = (n - 1) / 2;
		}
	}
	return x * y;
}
console.log(toPowerWithoutRecursion(2, 6)); // 8
console.log(toPowerWithoutRecursion(10, 2)); // 100

About

Find the power of the given base to the exponent without using native methods like math.pow

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0