Program to find number of squares in a chessboard

Given an n x n grid, count squares in it.

Examples :

Input: n = 2
Output: 5 (4 squares of 1 unit + 1 square of 2 units)

Input: n = 3
Output: 14 (9 squares of 1 unit + 4 square of 2 units 
                                + 1 square of 1 unit)

We strongly recommend you to minimize your browser and try this yourself first.

For a grid of size n*n the total number of squares formed are

1^2 + 2^2 + 3^2 + … + n^2 = n(n+1)(2n+1) / 6

Below is the implementation of above formula. Since the value of n*(n+1)*(2n+1) can cause overflow for large values of n, below are some interesting tricks used in the program.
1) long int is used in return.
2) n * (n + 1) / 2 is evaluated first as the value n*(n+1) will always be a multiple of 2.



Note that overflow may still happen, but above tricks just reduce chances of overflow.

C

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
// Function to return count of squares;
long int countSquares(int n)
{
    // A better way to write n*(n+1)*(2n+1)/6
    return (n * (n + 1) / 2) * (2*n + 1) / 3;
}
  
int main()
{
  int n = 4;
  cout << "Count of squares is " << countSquares(n);
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java  find number of squares in a 
// chessboard
  
  
class GFG 
{
    // Function to return count of squares;
    static int countSquares(int n)
    {
        // A better way to write n*(n+1)*(2n+1)/6
        return (n * (n + 1) / 2) * (2 * n + 1) / 3;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int n = 3;
        System.out.println("Count of squares is "
                           +countSquares(n));
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# python code to find number 
# of squares in a chessboard
  
# Function to return count
# of squares;
def countSquares(n):
      
    # better way to write
    # n*(n+1)*(2n+1)/6
    return ((n * (n + 1) / 2
           * (2 * n + 1) / 3)
      
# Driver code
n = 4
print("Count of squares is ",
              countSquares(n))
  
# This code is contributed by sam007.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# find number of squares in a 
// chessboard
using System;
  
public class GFG {
      
    static int countSquares(int n)
    {
        // A better way to write 
        // n*(n+1)*(2n+1)/6
        return (n * (n + 1) / 2) 
                * (2 * n + 1) / 3;
    }
      
    // Driver code
    public static void Main ()
    {
        int n = 4;
        Console.WriteLine("Count of"
                     + "squares is "
                 + countSquares(n));
    }
}
  
// This code is contributed by Sam007.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find number
// of squares in a chessboard
  
// Function to return
// count of squares;
function countSquares($n)
{
    // A better way to 
    // write n*(n+1)*(2n+1)/6
    return ($n * ($n + 1) / 2) * 
               (2 * $n + 1) / 3;
}
  
// Driver Code
$n = 4;
echo "Count of squares is " ,
            countSquares($n);
  
// This code is contributed 
// by nitin mittal.
?>

chevron_right


Output :

Count of squares is 30

This article is contributed by Rishabh. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up

Improved By : Sam007, nitin mittal