/*
Total squares in a 3 * 3 grid are 14
1x1 squares = 9 Squares
2x2 squares = 4 Squares
3x3 squares = 1 Squares
*/
object CountSquares {
// Function to calculate the total number of squares in an N x N grid
def countSquaresInNxNGrid(N: Int): Int = {
// Formula: Total Squares = N * (N + 1) * (2N + 1) / 6
(N * (N + 1) * (2 * N + 1)) / 6
}
def main(args: Array[String]): Unit = {
val N = 3
// Validate input
if (N <= 0) {
println("Grid size must be a positive integer!")
sys.exit(1) // Exit with error code
}
// Calculate and display the total number of squares
val totalSquares = countSquaresInNxNGrid(N)
println(s"The total number of squares in a ${N}x${N} grid is: $totalSquares")
}
}
/*
run:
The total number of squares in a 3x3 grid is: 14
*/