Created
August 26, 2023 21:40
-
-
Save nathanstilwell/81ca7bcdab70caefbd0c27a383be3dea to your computer and use it in GitHub Desktop.
Nathan Numerals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Nathan's Numerals */ | |
const NathanNumerals = [ | |
"•", "|", "—", | |
"+", "x", "=", | |
"△", "⊓", "ϟ", | |
"⏥", "⧖", "⩕", | |
] as const; | |
type NathanNumeralID = typeof NathanNumerals[number]; | |
const nanu: Record<NathanNumeralID, number> = { | |
"⩕": 117147, | |
"⧖": 59049, | |
"⏥": 19683, | |
"ϟ": 6561, | |
"⊓": 2187, | |
"△": 729, | |
"=": 243, | |
"x": 81, | |
"+": 27, | |
"—": 9, | |
"|": 3, | |
"•": 1, | |
} as const; | |
// this function is meant to be generic | |
const toNumerals = <T>( | |
num: number, | |
numerals: T, | |
numeralAccumulator: string[] = [], | |
) => { | |
if (numerals === undefined) { | |
throw new Error( | |
"Numeral set undefined. A base set of numerals must be supplied", | |
); | |
} | |
if (num < 0) { | |
return toNumerals(Math.abs(num), numerals, ["-", ...numeralAccumulator],); | |
} | |
const next = Object.keys(numerals).find((n) => Math.floor(num / numerals[n])); | |
console.log(next, numeralAccumulator); | |
if (next) { | |
return toNumerals( | |
num - numerals[next], | |
numerals, | |
[...numeralAccumulator, next], | |
); | |
} | |
return numeralAccumulator.join(""); | |
}; | |
const toNanu = (num: number) => { | |
// may need to add a way to reverse numeral systems | |
const nu = toNumerals(num, nanu); | |
return nu.split("").reverse().join(""); | |
} | |
// toNumerals<romanNumerals>(123, romanNumerals); | |
toNanu(152345) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment