Skip to content

Commit 6226595

Browse files
committed
climbing_stairs_bigint,fibonacci_bigint,pow_bigint
1 parent 529f5b1 commit 6226595

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

climbing-stairs/test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertStrictEquals } from "../deps.ts";
2-
import { climbing_stairs } from "../mod.ts";
2+
import { climbing_stairs, climbing_stairs_bigint } from "../mod.ts";
33

44
Deno.test("climbing-stairs", () => {
55
const examples: {
@@ -19,3 +19,21 @@ Deno.test("climbing-stairs", () => {
1919
assertStrictEquals(climbing_stairs(input), output);
2020
});
2121
});
22+
Deno.test("climbing_stairs_bigint", () => {
23+
const examples: {
24+
input: Parameters<typeof climbing_stairs_bigint>[0];
25+
output: ReturnType<typeof climbing_stairs_bigint>;
26+
}[] = [
27+
{ input: 10n, output: 89n },
28+
{ input: 1n, output: 1n },
29+
{ input: 2n, output: 2n },
30+
{ input: 3n, output: 3n },
31+
{ input: 45n, output: 1836311903n },
32+
{ input: 44n, output: 1134903170n },
33+
{ input: 46n, output: 1134903170n + 1836311903n },
34+
{ input: 99n, output: 354224848179261915075n },
35+
];
36+
examples.forEach(({ input, output }) => {
37+
assertStrictEquals(climbing_stairs_bigint(input), output);
38+
});
39+
});

fibonacci-number/test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertStrictEquals } from "../deps.ts";
2-
import { fibonacci_Number } from "../mod.ts";
2+
import { fibonacci_bigint, fibonacci_Number } from "../mod.ts";
33

44
Deno.test("fibonacci-number", () => {
55
const examples: {
@@ -17,3 +17,21 @@ Deno.test("fibonacci-number", () => {
1717
assertStrictEquals(fibonacci_Number(input), output);
1818
});
1919
});
20+
Deno.test("fibonacci_bigint", () => {
21+
const examples: {
22+
input: Parameters<typeof fibonacci_bigint>[0];
23+
output: ReturnType<typeof fibonacci_bigint>;
24+
}[] = [
25+
{ input: 2n, output: 1n },
26+
{ input: 3n, output: 2n },
27+
{ input: 4n, output: 3n },
28+
{ input: 30n, output: 832040n },
29+
{ input: 20n, output: 6765n },
30+
{ input: 21n, output: 10946n },
31+
{ input: 22n, output: 6765n + 10946n },
32+
{ input: 99n, output: 218922995834555169026n },
33+
];
34+
examples.forEach(({ input, output }) => {
35+
assertStrictEquals(fibonacci_bigint(input), output);
36+
});
37+
});

mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import merge_Sorted_Array from "./merge-sorted-array/index.ts";
22
import pow_x_n from "./powx-n/index.ts";
3+
export { pow_bigint } from "./powx-n/index.ts";
34

45
import html_Entity_Parser from "./html-entity-parser/index.ts";
56

powx-n/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,34 @@ export default function myPow(x: number, n: number): number {
6868
// return l * b();
6969
// }
7070
// }
71+
export function pow_bigint(x: bigint, n: bigint): bigint {
72+
if (n < 0) {
73+
throw Error("result not bigint:" + x + "," + n);
74+
}
75+
const result = n === 1n
76+
? x
77+
: x < 0
78+
? (n % 2n === 0n ? 1n : -1n) * pow_bigint(-x, n)
79+
: x === 1n
80+
? 1n
81+
: x === 0n
82+
? 0n
83+
: n === 0n
84+
? 1n
85+
: // : n < 0
86+
// ? pow_bigint(1 / x, -n)
87+
n % 2n
88+
? x * pow_bigint(x, n - 1n)
89+
: /* lazyMultiplyPositive(
90+
() => x,
91+
() => myPow(x, n - 1)
92+
) */
93+
pow_bigint(x * x, n / 2n);
94+
/* lazyMultiplyPositive(
95+
() => myPow(x, Math.floor(n / 2)),
96+
() => myPow(x, n - Math.floor(n / 2)),
97+
); */
98+
99+
// cacheStore.set(`${x},${n}`, result);
100+
return result;
101+
}

powx-n/test.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { assert } from "../deps.ts";
2-
import { pow_x_n } from "../mod.ts";
1+
import { assert, assertStrictEquals } from "../deps.ts";
2+
import { pow_bigint, pow_x_n } from "../mod.ts";
33
import { float64equals } from "../utils/float64equals.ts";
44

5-
Deno.test("powx-n", () => {
5+
Deno.test("pow_x-n", () => {
66
const examples: {
77
input: Parameters<typeof pow_x_n>;
88
output: ReturnType<typeof pow_x_n>;
@@ -42,3 +42,29 @@ Deno.test("powx-n", () => {
4242
assert(float64equals(output, pow_x_n(...input)));
4343
});
4444
});
45+
Deno.test("pow_bigint", () => {
46+
const examples: {
47+
input: Parameters<typeof pow_bigint>;
48+
output: ReturnType<typeof pow_bigint>;
49+
}[] = [
50+
{ input: [BigInt(2), 10n], output: BigInt(1024.0) },
51+
{
52+
input: [100n, 100n],
53+
output:
54+
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n,
55+
},
56+
57+
{
58+
input: [2n, 6n],
59+
output: BigInt(64.0),
60+
},
61+
{
62+
input: [688n, 68n],
63+
output:
64+
9036781882805851290639597927040482406482157574559116099548211283603471044068107755100184208020371364454930798169267683149572972315418389823766231220691565768687216159600116784986771438012727296n,
65+
},
66+
];
67+
examples.forEach(({ input, output }) => {
68+
assertStrictEquals(output, pow_bigint(...input));
69+
});
70+
});

0 commit comments

Comments
 (0)