Skip to content

Commit

Permalink
sqrtx/
Browse files Browse the repository at this point in the history
  • Loading branch information
masx200 committed Apr 10, 2022
1 parent cea5652 commit 0169f00
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ https://leetcode-cn.com/problems/unique-morse-code-words/

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

https://leetcode-cn.com/problems/sqrtx/

https://leetcode-cn.com/problems/jJ0w9p/

#### 安装教程

1. 安装`deno`
Expand Down
1 change: 1 addition & 0 deletions jJ0w9p/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../sqrtx/index.ts";
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ export { default as que_shi_de_shu_zi_lcof } from "./que-shi-de-shu-zi-lcof/inde
export { default as find_all_numbers_disappeared_in_an_array } from "./find-all-numbers-disappeared-in-an-array/index.ts";
export { default as move_zeros } from "./move-zeroes/index.ts";
export { default as unique_morse_code_words } from "./unique-morse-code-words/index.ts";
export { default as sqrt_x, SqrtNumber } from "./sqrtx/index.ts";
export { default as jJ0w9p } from "./jJ0w9p/index.ts";
33 changes: 33 additions & 0 deletions sqrtx/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default function mySqrt(x: number): number {
if (x < 0) {
throw Error("should greater than 0:" + x);
}
if (x === 0) {
return 0;
}
if (x === 1) {
return 1;
}
const delta = 1e-1;
const y = SqrtNumber(x, delta);
return Math.floor(Math.abs(y));
}
export function SqrtNumber(x: number, delta: number = Number.EPSILON): number {
if (x < 0) {
throw Error("should greater than 0:" + x);
}
if (x === 0) {
return 0;
}
if (x === 1) {
return 1;
}
// const delta = 1e-1;
let y = x;
let z = x;
while (z == x || Math.abs(z - y) > delta) {
z = y;
y = (x + y * y) / 2 / y;
}
return Math.abs(y);
}
59 changes: 59 additions & 0 deletions sqrtx/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { assertAlmostEquals, assertEquals } from "../deps.ts";
import { sqrt_x, SqrtNumber } from "../mod.ts";
Deno.test("sqrt-x", () => {
const examples: {
input: Parameters<typeof sqrt_x>[0];
output: ReturnType<typeof sqrt_x>;
}[] = [
{ input: 0, output: 0 },
{ input: 1, output: 1 },
{ input: 4, output: 2 },
{ input: 8, output: 2 },
{ input: 9, output: 3 },
{ input: 2, output: 1 },
{ input: 3, output: 1 },
{ input: 5, output: 2 },
{ input: 6, output: 2 },
{ input: 7, output: 2 },
{ input: 10, output: 3 },
{ input: 11, output: 3 },
{ input: 12, output: 3 },
{ input: 13, output: 3 },
{ input: 14, output: 3 },
{ input: 15, output: 3 },
{ input: 16, output: 4 },
{ input: 17, output: 4 },
];
examples.forEach(({ input, output }) => {
assertEquals(output, sqrt_x(input));
});
});

Deno.test("SqrtNumber", () => {
const examples: {
input: Parameters<typeof SqrtNumber>[0];
output: ReturnType<typeof SqrtNumber>;
}[] = [
{ input: 0, output: 0 },
{ input: 1, output: 1 },
{ input: 4, output: 2 },
{ input: 8, output: 2.8284271247461903 },
{ input: 9, output: 3 },
{ input: 2, output: 1.4142135623730951 },
{ input: 3, output: 1.7320508075688772 },
{ input: 5, output: 2.23606797749979 },
{ input: 6, output: 2.449489742783178 },
{ input: 7, output: 2.6457513110645907 },
{ input: 10, output: 3.1622776601683795 },
{ input: 11, output: 3.3166247903554 },
{ input: 12, output: 3.4641016151377544 },
{ input: 13, output: 3.605551275463989 },
{ input: 14, output: 3.7416573867739413 },
{ input: 15, output: 3.872983346207417 },
{ input: 16, output: 4 },
{ input: 17, output: 4.123105625617661 },
];
examples.forEach(({ input, output }) => {
assertAlmostEquals(output, SqrtNumber(input, 1e-10));
});
});

0 comments on commit 0169f00

Please sign in to comment.