Skip to content

Commit 089a112

Browse files
feat: add square root using Babylonian method
1 parent 5c39e87 commit 089a112

2 files changed

Lines changed: 62 additions & 45 deletions

File tree

‎Maths/SquareRoot.js‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
/*
2-
* Author: Rak Laptudirm
3-
*
4-
* https://en.wikipedia.org/wiki/Newton%27s_method
5-
*
6-
* Finding the square root of a number using Newton's method.
1+
/**
2+
* @function squareRoot
3+
* @description Calculates the square root of a non-negative number
4+
* using the Babylonian (Newton's) method without using Math.sqrt().
5+
* @param {number} n - A non-negative number
6+
* @param {number} precision - Decimal places of precision (default: 10)
7+
* @returns {number} - The square root of n
8+
* @see https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
9+
* @example squareRoot(4) => 2
10+
* @example squareRoot(9) => 3
11+
* @example squareRoot(2) => 1.4142135624
712
*/
8-
9-
function sqrt(num, precision = 4) {
10-
if (!Number.isFinite(num)) {
11-
throw new TypeError(`Expected a number, received ${typeof num}`)
13+
const squareRoot = (n, precision = 10) => {
14+
if (typeof n !== 'number' || isNaN(n) || n < 0) {
15+
throw new TypeError('Input must be a non-negative number')
1216
}
13-
if (!Number.isFinite(precision)) {
14-
throw new TypeError(`Expected a number, received ${typeof precision}`)
17+
if (!Number.isInteger(precision) || precision < 0) {
18+
throw new TypeError('Precision must be a non-negative integer')
1519
}
16-
let sqrt = 1
17-
for (let i = 0; i < precision; i++) {
18-
sqrt -= (sqrt * sqrt - num) / (2 * sqrt)
20+
if (n === 0 || n === 1) return n
21+
let guess = n / 2
22+
const epsilon = Math.pow(10, -precision)
23+
while (Math.abs(guess * guess - n) > epsilon) {
24+
guess = (guess + n / guess) / 2
1925
}
20-
return sqrt
26+
return parseFloat(guess.toFixed(precision))
2127
}
2228

23-
export { sqrt }
29+
export { squareRoot }

‎Maths/test/SquareRoot.test.js‎

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
1-
import { sqrt } from '../SquareRoot'
1+
import { squareRoot } from '../SquareRoot.js'
22

3-
test('Check SquareRoot of 4 is 2', () => {
4-
const res = sqrt(4, 10)
5-
expect(res).toBeCloseTo(2)
6-
})
3+
describe('squareRoot', () => {
4+
test('squareRoot(0) returns 0', () => {
5+
expect(squareRoot(0)).toBe(0)
6+
})
77

8-
test('Check SquareRoot of 2 is 1.4142135', () => {
9-
const res = sqrt(2, 10)
10-
expect(res).toBeCloseTo(1.4142135)
11-
})
8+
test('squareRoot(1) returns 1', () => {
9+
expect(squareRoot(1)).toBe(1)
10+
})
1211

13-
test('Check SquareRoot of 3.2 is 1.788854381999832', () => {
14-
const res = sqrt(3.2, 10)
15-
expect(res).toBeCloseTo(1.788854381999832)
16-
})
12+
test('squareRoot(4) returns 2', () => {
13+
expect(squareRoot(4)).toBe(2)
14+
})
1715

18-
test('Check SquareRoot of 1 is 1', () => {
19-
const res = sqrt(1, 10)
20-
expect(res).toBe(1)
21-
})
16+
test('squareRoot(9) returns 3', () => {
17+
expect(squareRoot(9)).toBe(3)
18+
})
2219

23-
test('Check SquareRoot of 144 is 12', () => {
24-
const res = sqrt(144, 10)
25-
expect(res).toBeCloseTo(12)
26-
})
20+
test('squareRoot(16) returns 4', () => {
21+
expect(squareRoot(16)).toBe(4)
22+
})
2723

28-
test('Check SquareRoot of 0 is 0', () => {
29-
const res = sqrt(0, 10)
30-
expect(res).toBeCloseTo(0)
31-
})
24+
test('squareRoot(100) returns 10', () => {
25+
expect(squareRoot(100)).toBe(10)
26+
})
27+
28+
test('squareRoot(2) returns approximately 1.4142135624', () => {
29+
expect(squareRoot(2)).toBeCloseTo(1.4142135624, 5)
30+
})
31+
32+
test('squareRoot(0.25) returns 0.5', () => {
33+
expect(squareRoot(0.25)).toBeCloseTo(0.5, 5)
34+
})
35+
36+
test('throws TypeError for negative input', () => {
37+
expect(() => squareRoot(-1)).toThrow(TypeError)
38+
})
39+
40+
test('throws TypeError for non-number input', () => {
41+
expect(() => squareRoot('4')).toThrow(TypeError)
42+
})
3243

33-
test('Check SquareRoot of 1000 is 31.62277', () => {
34-
const res = sqrt(1000, 10)
35-
expect(res).toBeCloseTo(31.62277)
44+
test('throws TypeError for NaN', () => {
45+
expect(() => squareRoot(NaN)).toThrow(TypeError)
46+
})
3647
})

0 commit comments

Comments
 (0)