|
1 |
| -// Returns the value of x to the power of y |
| 1 | +/** |
| 2 | + * @function powLinear |
| 3 | + * @description - The powLinear function is a power function with Linear O(n) complexity |
| 4 | + * @param {number} base |
| 5 | + * @param {number} exponent |
| 6 | + * @returns {number} |
| 7 | + * @example - powLinear(2, 2) => 4 --> 2 * 2 |
| 8 | + * @example - powLinear(3, 3) => 27 --> 3 * 3 |
| 9 | + */ |
| 10 | +const powLinear = (base, exponent) => { |
| 11 | + if (exponent < 0) { |
| 12 | + base = 1 / base |
| 13 | + exponent = -exponent |
| 14 | + } |
2 | 15 |
|
3 |
| -const pow = (x, y) => { |
4 | 16 | let result = 1
|
5 |
| - for (let i = 1; i <= y; i++) { |
6 |
| - result *= x |
| 17 | + |
| 18 | + while (exponent--) { // Break the execution while the exponent will 0 |
| 19 | + result *= base |
7 | 20 | }
|
| 21 | + |
8 | 22 | return result
|
9 | 23 | }
|
10 | 24 |
|
11 |
| -export { pow } |
| 25 | +/** |
| 26 | + * @function powFaster |
| 27 | + * @description - The powFaster function is a power function with O(logN) complexity |
| 28 | + * @param {number} base |
| 29 | + * @param {number} exponent |
| 30 | + * @returns {number} |
| 31 | + * @example - powFaster(2, 2) => 4 --> 2 * 2 |
| 32 | + * @example - powFaster(3, 3) => 27 --> 3 * 3 |
| 33 | + */ |
| 34 | +const powFaster = (base, exponent) => { |
| 35 | + if (exponent < 2) { // explanation below - 1 |
| 36 | + return base && ([1, base][exponent] || powFaster(1 / base, -exponent)) |
| 37 | + } |
| 38 | + |
| 39 | + if (exponent & 1) { // if the existing exponent is odd |
| 40 | + return base * powFaster(base * base, exponent >> 1) // explanation below - 2 |
| 41 | + } |
| 42 | + |
| 43 | + return powFaster(base * base, exponent / 2) |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * 1 - Magic of short circuit evaluation (&&, ||) |
| 48 | + * if the base is 0 then it returns 0 cause 0 is falsy |
| 49 | + * if the base is not 0 then it's must be truthy. after that, it will be executed the right portion of the && (AND) operator |
| 50 | + * Now it checks the exponent by the help array index, is it 0 or 1. |
| 51 | + * if the exponent is not 0 or 1 it's definitely less than 0, and a negative number is not a valid index number so it returns "undefined" |
| 52 | + * if the expression is undefined mean -> falsy, the || (OR) operator evaluates the right portion that is a recursive function. |
| 53 | + */ |
| 54 | + |
| 55 | +/** |
| 56 | + * 2 - Play with right shift bitwise operator (>>) |
| 57 | + * right shift with any odd numbers it returns the floor number instead of float. |
| 58 | + * E.g. if the number is 5, after right shifting with 1 it's will give us 2, not 2.5 |
| 59 | + * cause the right shift formula is --> x >> y = |x| / 2^y |
| 60 | + */ |
| 61 | + |
| 62 | +export { powLinear, powFaster } |
0 commit comments