I want to invert this sequence:
Invert a function like in mathematics(is this exist in programming)
What I mean: if f(x)=y --->g(y)=x so g is the inverse function of f.
My attempt was(but still not working):
function seq(num) {
if(num < 2) {return 1; }
if(num === 2) {return 2; }
if(num % 2 === 1) {
const t = (num - 1) / 2;
return seq(t - 1) + seq(t) + 1;
}
const t = num / 2;
return seq(t) + seq(t + 1) + t;
}
document.write(_.invert(seq(4)));
There is no known algorithm to programmatically invert a function, baring brute force over the domain. Indeed, such an algorithm, if workable would be a stunning discovery, likely breaking the vast majority of cryptosystems. There are a bunch of different methods for finding the inverse of a function, but they depend on the function in question meeting specific criteria.
The lodash invert function performs an completely different function, swapping keys and values of objects.