-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpositional-number-system.ts
51 lines (45 loc) · 1.35 KB
/
positional-number-system.ts
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
42
43
44
45
46
47
48
49
50
51
export type PNSFunctions = {
isValid: (str: string) => boolean
encode: (num: bigint) => string
decode: (str: string) => bigint
}
export function positionalNumberSystem(base: bigint, alphabet: string): PNSFunctions {
if (BigInt(alphabet.length) !== base) {
throw new Error(
`Alphabet length is ${alphabet.length}, but base is ${base})`)
}
function isValid(str: string): boolean {
for (let i = 0; i < str.length; i++) {
if (alphabet.indexOf(str[i]) === -1) {
return false
}
}
return true
}
function encode(num: bigint): string {
if (num === 0n) {
return alphabet[0]
}
let str = ''
while (num > 0) {
const quotient = num / base
const remainder = num % base
str = alphabet[Number(remainder)] + str
num = quotient
}
return str
}
function decode(str: string): bigint {
let num = 0n
for (let i = 0; i < str.length; i++) {
const char = str[i]
const index = alphabet.indexOf(char)
if (index === -1) {
throw new Error('Invalid character: ' + char)
}
num = num * base + BigInt(index)
}
return num
}
return { isValid, encode, decode }
}