formula for number of squares in a grid code example

Example 1: equation for amount of squares in a grid

4x3 = 20 4x4 = 30 4x5 = 40 4x6 = 50 4x7 = 60 4x8 = 70 4x9 = 80// Guess what is the next value!

Example 2: equation for amount of squares in a grid

/** * I'm using my own versions of max and min because * Math package only has max() float64 and min() float64 */func max (a,b int) int {  if a > b {  return a  } else {  return b  }  return 0}func min (a,b int) int {  if a < b {  return a  } else {  return b  }  return 0}func count(n, m int) int { s, b := min(n, m), max(n, m) r := 0 for i := 1; i <= s; i++ { r += (s - i + 1) * (b - i + 1) } return r}func main() { fmt.Print(count(2,3));}

Tags:

Misc Example