DEV Community

Discussion on: Daily Challenge #112 - Functions of Integers on the Cartesian Plane

Collapse
 
craigmc08 profile image
Craig McIlwrath

No loops required!

summin n = n * (n + 1) * (2*n + 1) `div` 6

summax n = n*n*n - summin (n - 1)

sumsum n = n*n*n + n*n

I'll put my thought process here because those equations look pretty arbitrary.

From that example grid of values of f, you can see the sum is 1*(n2 - (n-1)2) + 2 * ((n-1)2 - (n-2)2) + ... when you expand that out you get the sum of the first n squares. Which is the formula in summin (that Google lead me to).

For g, you can produce a chart of its value by taking an nxn square of n's, then subtracting the area of each square smaller than that. e.g. n*n*n - (n - 1)2 - (n - 2)2 -... which is the same as n*n*n - summin (n-1)

sumsum could be just the sum of the other two functions, but that's too simple. if you math it out, you get n*n*n + n*n. Much better.