Conversation
…rea, and binary exponentiation functions
appgurueu
left a comment
There was a problem hiding this comment.
- All the area calculation functions are algorithmically pretty uninteresting formulae and should thus be removed IMO. Same for the geometric mean, though that might be more useful. I'm slightly on the fence about the triangle inequality checking; it's probably fine either way.
- Please get rid of redundant
@functiondoc comments. - Please get rid of unrelated changes:
package.json,package-lock.json
| /** | ||
| * @function binaryExponent | ||
| * @description Returns the power of a number A raised to the power of B with binary exponentiation | ||
| * @summary Binary exponentiation calculatoes A^B in logarithmic time of B instead of linear time |
There was a problem hiding this comment.
| * @summary Binary exponentiation calculatoes A^B in logarithmic time of B instead of linear time | |
| * @summary Binary exponentiation calculates A^B in time O(log B) instead of O(B) |
| * @function binaryExponent | ||
| * @description Returns the power of a number A raised to the power of B with binary exponentiation | ||
| * @summary Binary exponentiation calculatoes A^B in logarithmic time of B instead of linear time | ||
| * @param {Number} num - An array of two natural numbers, [A, B], where A^B will be solved |
There was a problem hiding this comment.
Just take two numbers, base and exponent, as parameters instead.
| * @summary Binary exponentiation calculatoes A^B in logarithmic time of B instead of linear time | ||
| * @param {Number} num - An array of two natural numbers, [A, B], where A^B will be solved | ||
| * @return {number} A^B | ||
| * @see [Wikipedia](https://cp-algorithms.com/algebra/binary-exp.html) |
| throw new TypeError('Invalid Input') | ||
| } | ||
| for(let i=0; i < numbers.length; i++){ | ||
| if (numbers[i] < 0 || !Number.isInteger(numbers[i])) { |
There was a problem hiding this comment.
The base can very well be negative. You could also support negative exponents but you don't have to.
| export const binaryExponent = (numbers: number[]): number => { | ||
| // raise errors upon invalid inputs | ||
| if (numbers.length != 2) { | ||
| throw new TypeError('Invalid Input') |
There was a problem hiding this comment.
This becomes obsolete if you fix the signature.
| console.log(factors1) | ||
|
|
||
| // check if same factors | ||
| for (let i = 0; i <= factors0.length; i++) { |
There was a problem hiding this comment.
Could be optimized e.g. by doing a sorted merge instead. This is O(nm) but could be O(n + m). Not really necessary since the factorization is more expensive though (which should be noted however).
| import { binaryExponent } from '../binary_exponentiation' | ||
|
|
||
| describe('Tests for HexArea', () => { | ||
| it('should be a function', () => { |
| @@ -0,0 +1,29 @@ | |||
| import { binaryExponent } from '../binary_exponentiation' | |||
|
|
|||
| describe('Tests for HexArea', () => { | |||
There was a problem hiding this comment.
| describe('Tests for HexArea', () => { | |
| describe('HexArea', () => { |
| @@ -0,0 +1,25 @@ | |||
| import { coPrime } from '../co_prime' | |||
|
|
|||
| describe('Tests for CoPrime', () => { | |||
There was a problem hiding this comment.
| describe('Tests for CoPrime', () => { | |
| describe('CoPrime', () => { |
| import { coPrime } from '../co_prime' | ||
|
|
||
| describe('Tests for CoPrime', () => { | ||
| it('should be a function', () => { |
|
Please remove the coprime check: #250 was opened earlier and implements a strictly better algorithm, obsoleting this one. |
Includes changes related to Issue: #10
Included new maths functions and tests to calculate the geometric mean, triangle inequality, coprime, binary exponentiation, and area of regular hexagon, pentagon, and octagon, plus the tests for each.