Elegant challenge. We can notice that f and g are complementary to each other. We can draw the grid for f and g and notice, that sum of these grids is equal to sum of the gride of size n*n with (n+1) value is each cell. So the sum of f and g is equal to n*n*(n+1). The sum h is also equal to f + g.
fun fghSum(n: Long) = n * n * (n + 1) * 2
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.