FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Maths/SquareRootLogarithmic.js at master · MMABSOUT/JavaScript · GitHub
MMABSOUT
/
JavaScript
Public
forked from
TheAlgorithms/JavaScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Maths
/
SquareRootLogarithmic.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
41 lines (38 loc) · 975 Bytes
Breadcrumbs
JavaScript
/
Maths
/
SquareRootLogarithmic.js
Copy path
File metadata and controls
41 lines (38 loc) · 975 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
answer
=
0
let
sqrt
=
0
let
edge
=
num
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
{
edge
=
mid
-
1
}
}
return
answer
}
export
{
squareRootLogarithmic
}
Back
|
FazBrowse Home
|
New Git URL