Add memoization to f() function to save computing it many times