From 8ca0efc133cca6bf5110f17caee28bef31df5ca9 Mon Sep 17 00:00:00 2001 From: alex_p Date: Mon, 31 Oct 2022 14:43:16 +0300 Subject: [PATCH 1/3] algorithm: add SquareRootLogarithmic algo and a test for it --- Maths/SquareRootLogarithmic.js | 41 ++++++++++++++++++++++++ Maths/test/SquareRootLogarithmic.test.js | 13 ++++++++ 2 files changed, 54 insertions(+) create mode 100644 Maths/SquareRootLogarithmic.js create mode 100644 Maths/test/SquareRootLogarithmic.test.js diff --git a/Maths/SquareRootLogarithmic.js b/Maths/SquareRootLogarithmic.js new file mode 100644 index 0000000000..e272e8e66c --- /dev/null +++ b/Maths/SquareRootLogarithmic.js @@ -0,0 +1,41 @@ +/** + * @function squareRootLogarithmic + * @description + * Return the square root of 'num' rounded down + * to the nearest integer. + * More info: https://leetcode.com/problems/sqrtx/ + * @param {Number} num Number whose square of root is to be found + * @returns {Number} Square root + * @see [BinarySearch](https://en.wikipedia.org/wiki/Binary_search_algorithm) + * @example + * const num1 = 4 + * logarithmicSquareRoot(num1) // ====> 2 + * @example + * const num2 = 8 + * logarithmicSquareRoot(num1) // ====> 2 + * + */ +const squareRootLogarithmic = (num) => { + if (typeof num !== 'number') { + throw new Error('Input data must be numbers') + } + let ans = 0 + let sqrt = 0 + let e = num + + while (sqrt <= e) { + const mid = Math.trunc((sqrt + e) / 2) + if (mid * mid === num) { + return mid + } else if (mid * mid < num) { + sqrt = mid + 1 + ans = mid + } else { + e = mid - 1 + } + } + + return ans +} + +export { squareRootLogarithmic } diff --git a/Maths/test/SquareRootLogarithmic.test.js b/Maths/test/SquareRootLogarithmic.test.js new file mode 100644 index 0000000000..6eec49d239 --- /dev/null +++ b/Maths/test/SquareRootLogarithmic.test.js @@ -0,0 +1,13 @@ +import { squareRootLogarithmic } from '../SquareRootLogarithmic' + +describe('SquareRootLogarithmic', () => { + test('Finding the square root of a positive integer', () => { + expect(squareRootLogarithmic(4)).toEqual(2) + expect(squareRootLogarithmic(16)).toEqual(4) + expect(squareRootLogarithmic(8)).toEqual(2) + }) + test('Throwing an exception', () => { + expect(() => squareRootLogarithmic('not a number')).toThrow() + expect(() => squareRootLogarithmic(true)).toThrow() + }) +}) From 2e56401ae3ee89f0870427752221358543a5436c Mon Sep 17 00:00:00 2001 From: alex_p Date: Mon, 31 Oct 2022 14:51:09 +0300 Subject: [PATCH 2/3] fix: fix spelling errors --- Maths/SquareRootLogarithmic.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Maths/SquareRootLogarithmic.js b/Maths/SquareRootLogarithmic.js index e272e8e66c..bd2c123c37 100644 --- a/Maths/SquareRootLogarithmic.js +++ b/Maths/SquareRootLogarithmic.js @@ -19,7 +19,7 @@ const squareRootLogarithmic = (num) => { if (typeof num !== 'number') { throw new Error('Input data must be numbers') } - let ans = 0 + let answer = 0 let sqrt = 0 let e = num @@ -29,13 +29,13 @@ const squareRootLogarithmic = (num) => { return mid } else if (mid * mid < num) { sqrt = mid + 1 - ans = mid + answer = mid } else { e = mid - 1 } } - return ans + return answer } export { squareRootLogarithmic } From 39c9807c75bde0c49189cbfb404eb2d193f51c79 Mon Sep 17 00:00:00 2001 From: alex_p Date: Mon, 31 Oct 2022 16:04:48 +0300 Subject: [PATCH 3/3] refactor: rename a variable "e" --> "edge" --- Maths/SquareRootLogarithmic.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/SquareRootLogarithmic.js b/Maths/SquareRootLogarithmic.js index bd2c123c37..e9b54aed37 100644 --- a/Maths/SquareRootLogarithmic.js +++ b/Maths/SquareRootLogarithmic.js @@ -21,17 +21,17 @@ const squareRootLogarithmic = (num) => { } let answer = 0 let sqrt = 0 - let e = num + let edge = num - while (sqrt <= e) { - const mid = Math.trunc((sqrt + e) / 2) + while (sqrt <= edge) { + const mid = Math.trunc((sqrt + edge) / 2) if (mid * mid === num) { return mid } else if (mid * mid < num) { sqrt = mid + 1 answer = mid } else { - e = mid - 1 + edge = mid - 1 } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy