Last active
December 23, 2020 14:25
-
-
Save manavm1990/fc2f1d197247fdf8013e01c45279e6cb to your computer and use it in GitHub Desktop.
Eloquent Functions
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
const exponential = (base: number, exponent: number): number => | |
exponent === 0 ? 1 : base * exponential(base, exponent - 1); |
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
const calcPhi = ( | |
table: // TODO: Find a 'cleaner' way to express this! | |
[number, number, number, number] | |
): number => | |
(table[3] * table[0] - table[2] * table[1]) / | |
Math.sqrt( | |
(table[2] + table[3]) * | |
(table[0] + table[1]) * | |
(table[1] + table[3]) * | |
(table[0] + table[2]) | |
); |
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
/** | |
* TODO: By starting from the number 1 and | |
* repeatedly either adding 5 or multiplying by 3, | |
* an infinite set of numbers can be produced. | |
* How would you write a function that, given a number, | |
* tries to find a sequence of such additions and | |
* multiplications that produces that number? | |
*/ | |
const trace = (config: { | |
current: number; | |
target: number; | |
history: string; | |
}): string | null => { | |
// Dun! | |
if (config.current === config.target) { | |
return config.history; | |
} | |
// Over shot - trigger recursive unwinding by returning 'falsy' | |
if (config.current > config.target) { | |
return null; | |
} | |
return ( | |
trace({ | |
current: config.current + 5, | |
target: config.target, | |
history: `(${config.history} + 5)`, | |
}) || | |
trace({ | |
current: config.current * 3, | |
target: config.target, | |
history: `(${config.history} * 3)`, | |
}) | |
); | |
}; | |
console.log(trace({ current: 1, target: 24, history: "1" })); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment