But then I figured that f as a whole can’t work, really. For example, when n is 8, the term ((3^g(n)) - 1) / 2 in the fourth line of f would have to be 3 but there is no value of g() that can make this happen.
It's the drawing that was buggy, not the function. See this post for the correct drawing.
Sorry that this error wasted your time too.