Skip to content

feat: add square root #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
* [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts)
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts)
* [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts)
* [Square Root](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/square_root.ts)
* [Ugly Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/ugly_numbers.ts)
* [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts)

Expand Down
26 changes: 26 additions & 0 deletions maths/square_root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @function squareRoot
* @description Finding the square root of a number using Newton's method.
* @param {number} num - A number.
* @param {number} precision - Precision of square root, 1e-15 by default.
* @returns {number} - Square root of the given number.
* @see https://www.geeksforgeeks.org/find-root-of-a-number-using-newtons-method/
* @example SquareRoot(36) = 6
* @example SquareRoot(50) = 7.0710678118654755
*/

export const squareRoot = (num: number, precision: number = 1e-15): number => {
if (num < 0) throw new Error("number must be non-negative number");
if (num === 0) return 0;

let sqrt: number = num;
let curr: number;

while (true) {
curr = 0.5 * (sqrt + num / sqrt);
if (Math.abs(curr - sqrt) < precision) {
return sqrt;
}
sqrt = curr;
}
};
28 changes: 28 additions & 0 deletions maths/test/square_root.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { squareRoot } from "../square_root";

describe("squareRoot", () => {
test.each([-1, -10, -2.4])(
"should throw an error for negative numbers",
(n: number) => {
expect(() => squareRoot(n)).toThrow("number must be non-negative number");
}
);

test.each([0, 1, 4, 9, 16, 25])(
"should return correct rational square root value",
() => {
(n: number) => {
expect(() => squareRoot(n)).toBeCloseTo(Math.sqrt(n));
};
}
);

test.each([2, 15, 20, 40, 99, 10032])(
"should return correct irrational square root value",
() => {
(n: number) => {
expect(() => squareRoot(n)).toBeCloseTo(Math.sqrt(n));
};
}
);
});
14 changes: 9 additions & 5 deletions maths/test/ugly_numbers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { UglyNumbers } from '../ugly_numbers';
import { uglyNumbers } from "../ugly_numbers";

test('Ugly Numbers', () => {
const uglyNumbers = UglyNumbers();
expect(Array(11).fill(undefined).map(() => uglyNumbers.next().value)).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]);
})
test("Ugly Numbers", () => {
const uglyNums = uglyNumbers();
expect(
Array(11)
.fill(undefined)
.map(() => uglyNums.next().value)
).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]);
});
38 changes: 20 additions & 18 deletions maths/ugly_numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,34 @@
* They can be represented in the form 2^a * 3^b * 5*c. By convention, 1 is also considered to be
* an ugly number.
* The first few terms of the sequence are: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20...
*
*
* For the provided n, the nth ugly number shall be computed.
* @see [GeeksForGeeks](https://www.geeksforgeeks.org/ugly-numbers/)
*/
function* UglyNumbers() {
yield 1
function* uglyNumbers() {
yield 1;

let idx2 = 0, idx3 = 0, idx5 = 0
const uglyNumbers = [1]
let idx2 = 0,
idx3 = 0,
idx5 = 0;
const uglyNums: number[] = [1];

let nextx2: number, nextx3: number, nextx5: number, nextUglyNum: number
let nextx2: number, nextx3: number, nextx5: number, nextUglyNum: number;

while(true) {
nextx2 = uglyNumbers[idx2] * 2
nextx3 = uglyNumbers[idx3] * 3
nextx5 = uglyNumbers[idx5] * 5
while (true) {
nextx2 = uglyNums[idx2] * 2;
nextx3 = uglyNums[idx3] * 3;
nextx5 = uglyNums[idx5] * 5;

nextUglyNum = Math.min(nextx2, nextx3, nextx5)
yield nextUglyNum
nextUglyNum = Math.min(nextx2, nextx3, nextx5);
yield nextUglyNum;

if(nextx2 === nextUglyNum) idx2++
if(nextx3 === nextUglyNum) idx3++
if(nextx5 === nextUglyNum) idx5++
uglyNumbers.push(nextUglyNum)
if (nextx2 === nextUglyNum) idx2++;
if (nextx3 === nextUglyNum) idx3++;
if (nextx5 === nextUglyNum) idx5++;

uglyNums.push(nextUglyNum);
}
}

export { UglyNumbers }
export { uglyNumbers };