Skip to content

Commit 0169f00

Browse files
committed
sqrtx/
1 parent cea5652 commit 0169f00

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ https://leetcode-cn.com/problems/unique-morse-code-words/
3636

3737
https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/
3838

39+
https://leetcode-cn.com/problems/sqrtx/
40+
41+
https://leetcode-cn.com/problems/jJ0w9p/
42+
3943
#### 安装教程
4044

4145
1. 安装`deno`

jJ0w9p/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "../sqrtx/index.ts";

mod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ export { default as que_shi_de_shu_zi_lcof } from "./que-shi-de-shu-zi-lcof/inde
3030
export { default as find_all_numbers_disappeared_in_an_array } from "./find-all-numbers-disappeared-in-an-array/index.ts";
3131
export { default as move_zeros } from "./move-zeroes/index.ts";
3232
export { default as unique_morse_code_words } from "./unique-morse-code-words/index.ts";
33+
export { default as sqrt_x, SqrtNumber } from "./sqrtx/index.ts";
34+
export { default as jJ0w9p } from "./jJ0w9p/index.ts";

sqrtx/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export default function mySqrt(x: number): number {
2+
if (x < 0) {
3+
throw Error("should greater than 0:" + x);
4+
}
5+
if (x === 0) {
6+
return 0;
7+
}
8+
if (x === 1) {
9+
return 1;
10+
}
11+
const delta = 1e-1;
12+
const y = SqrtNumber(x, delta);
13+
return Math.floor(Math.abs(y));
14+
}
15+
export function SqrtNumber(x: number, delta: number = Number.EPSILON): number {
16+
if (x < 0) {
17+
throw Error("should greater than 0:" + x);
18+
}
19+
if (x === 0) {
20+
return 0;
21+
}
22+
if (x === 1) {
23+
return 1;
24+
}
25+
// const delta = 1e-1;
26+
let y = x;
27+
let z = x;
28+
while (z == x || Math.abs(z - y) > delta) {
29+
z = y;
30+
y = (x + y * y) / 2 / y;
31+
}
32+
return Math.abs(y);
33+
}

sqrtx/test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { assertAlmostEquals, assertEquals } from "../deps.ts";
2+
import { sqrt_x, SqrtNumber } from "../mod.ts";
3+
Deno.test("sqrt-x", () => {
4+
const examples: {
5+
input: Parameters<typeof sqrt_x>[0];
6+
output: ReturnType<typeof sqrt_x>;
7+
}[] = [
8+
{ input: 0, output: 0 },
9+
{ input: 1, output: 1 },
10+
{ input: 4, output: 2 },
11+
{ input: 8, output: 2 },
12+
{ input: 9, output: 3 },
13+
{ input: 2, output: 1 },
14+
{ input: 3, output: 1 },
15+
{ input: 5, output: 2 },
16+
{ input: 6, output: 2 },
17+
{ input: 7, output: 2 },
18+
{ input: 10, output: 3 },
19+
{ input: 11, output: 3 },
20+
{ input: 12, output: 3 },
21+
{ input: 13, output: 3 },
22+
{ input: 14, output: 3 },
23+
{ input: 15, output: 3 },
24+
{ input: 16, output: 4 },
25+
{ input: 17, output: 4 },
26+
];
27+
examples.forEach(({ input, output }) => {
28+
assertEquals(output, sqrt_x(input));
29+
});
30+
});
31+
32+
Deno.test("SqrtNumber", () => {
33+
const examples: {
34+
input: Parameters<typeof SqrtNumber>[0];
35+
output: ReturnType<typeof SqrtNumber>;
36+
}[] = [
37+
{ input: 0, output: 0 },
38+
{ input: 1, output: 1 },
39+
{ input: 4, output: 2 },
40+
{ input: 8, output: 2.8284271247461903 },
41+
{ input: 9, output: 3 },
42+
{ input: 2, output: 1.4142135623730951 },
43+
{ input: 3, output: 1.7320508075688772 },
44+
{ input: 5, output: 2.23606797749979 },
45+
{ input: 6, output: 2.449489742783178 },
46+
{ input: 7, output: 2.6457513110645907 },
47+
{ input: 10, output: 3.1622776601683795 },
48+
{ input: 11, output: 3.3166247903554 },
49+
{ input: 12, output: 3.4641016151377544 },
50+
{ input: 13, output: 3.605551275463989 },
51+
{ input: 14, output: 3.7416573867739413 },
52+
{ input: 15, output: 3.872983346207417 },
53+
{ input: 16, output: 4 },
54+
{ input: 17, output: 4.123105625617661 },
55+
];
56+
examples.forEach(({ input, output }) => {
57+
assertAlmostEquals(output, SqrtNumber(input, 1e-10));
58+
});
59+
});

0 commit comments

Comments
 (0)