find the number of 11x11 squares in a 55x55 square grid?
Mohammed
Guys, does anyone know the answer?
get find the number of 11x11 squares in a 55x55 square grid? from screen.
The Number of Lattice Squares
What is the total number of squares that can fit into an n x n grid? *Lattice squares are the squares whose vertices are on the grid points. There are two types of lattice squares, grid ones and the tilted ones.
Eda Aydemir Jun 25, 2021 4 min read
The Number of Lattice Squares*
There are many puzzles about the number squares you can draw by using the grid points ( lattice points) on a given grid. Here is an example;What is the total number of squares that can fit into an n x n grid?
*Lattice squares are the squares whose vertices are on the grid points. There are two types of lattice squares, grid ones and the tilted ones.Let’s define a "grid square" as a square whose vertices are lattice points and sides are along the axis. (vertical squares). They are easy to create and have square number areas.SOLUTION
We can start solving this puzzle by remembering another one! Famous" Checkerboard Puzzle".The answer of the Checkerboard Problem gives us the number of grid squares. To be able to find the total number of squares on a checkerboard, we need to consider that the board has 2 x 2 squares, 3 x 3 squares, 4 x 4 squares and so on other than 64 unit squares.Find the number of squares inside the given square grid
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Find the number of squares inside the given square grid
Last Updated : 08 Mar, 2022
Read Discuss Courses Practice Video
Given a grid of side N * N, the task is to find the total number of squares that exist inside it. All squares selected can be of any length.
Examples:Input: N = 1Output: 1Recommended: Please try your approach on first, before moving on to the solution.
Approach 1: Taking a few examples, it can be observed that for a grid on size N * N, the number of squares inside it will be 12 + 22 + 32 + … + N2Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#includeusing namespace std;
// Function to return the number
// of squares inside an n*n grid
int cntSquares(int n)
{ int squares = 0;
for (int i = 1; i <= n; i++) {
squares += pow(i, 2);
} return squares; } // Driver code int main() { int n = 4;
cout << cntSquares(4);
return 0; }
Java
// Java implementation of the approach
class GFG {
// Function to return the number
// of squares inside an n*n grid
static int cntSquares(int n)
{ int squares = 0;
for (int i = 1; i <= n; i++) {
squares += Math.pow(i, 2);
} return squares; } // Driver code
public static void main(String args[])
{ int n = 4;
System.out.print(cntSquares(4));
} }
Python3
# Python3 implementation of the approach
# Function to return the number
# of squares inside an n*n grid
def cntSquares(n) : squares = 0;
for i in range(1, n + 1) :
squares += i ** 2; return squares; # Driver code
if __name__ == "__main__" :
n = 4;
print(cntSquares(4));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System; class GFG {
// Function to return the number
// of squares inside an n*n grid
static int cntSquares(int n)
{ int squares = 0;
for (int i = 1; i <= n; i++)
{
squares += (int)Math.Pow(i, 2);
} return squares; } // Driver code
public static void Main(String []args)
{ int n = 4;
Console.Write(cntSquares(n));
} }
// This code is contributed by 29AjayKumar
Javascript
Output:30
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach 2: By the use of direct formula.However, the sum has the closed form (direct formula) . Hence, we can employ this to calculate the sum in time.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#includeusing namespace std;
int cnt_squares (int n)
{
/* Function to return the number
of squares inside an n*n grid */
return n * (n + 1) * (2 * n + 1) / 6;
} // Driver code int main() {
cout << cnt_squares (4) << endl;
return 0; }
Java
// Java implementation of the approach
class GFG {
static int cntSquares (int n) {
/* Function to return the number
of squares inside an n*n grid */
return n * (n + 1) * (2 * n + 1) / 6;
} // Driver code
public static void main(String args[]) {
System.out.println (cntSquares(4));
} }
Python3
# Python3 implementation of the approach
"""
Function to return the number
of squares inside an n*n grid
""" def cntSquares(n) :
return int (n * (n + 1) * (2 * n + 1) / 6)
# Driver code
if __name__ == "__main__" :
print (cntSquares (4));
C#
// C# implementation of the approach
using System; class GFG {
/* Function to return the number
of squares inside an n*n grid */
static int cntSquares (int n)
{
return n * (n + 1) * (2 * n + 1) / 6;
} // Driver code
public static void Main (String[] args)
{
Console.Write (cntSquares (4));
} }
Javascript
Find number of squares inside a given square grid in C++
Here we will check out how to find the number of squares inside a given square grid in C++ using simple formula with the corresponding code.
Find number of squares inside a given square grid in C++
By HARISHWAR REDDY MUNUKUNTLA
In this tutorial, we will check out how to find the number of squares inside a given square grid in C++ with the corresponding code.
If the square grid contains a side of N*N, then we are going to find the total number of squares present in it.
calculate the number of squares inside a square grid in C++
If we observe the number of squares that exist in smaller square grids then we can draw a pattern that will guide us to construct a simple formula to find the number of squares in a square grid of any length.
The total number of squares in a square grid of side 1 =1.
Total number of squares in a square grid of side 2 =5. ⇒ 4 small squares, and 1 2×2 square.
The total number of squares in a square grid of side 3 =14. ⇒ 9 small squares, 4 2×2 squares and 1 3×3 square.
The total number of squares in a square grid of side 4 =30. ⇒ 16 small squares, 9 3×3 squares, 4 2×2 squares and 1 4×4 square.
By observing above sentences we can draw a pattern1×1 :-1^2 = 1.
2×2 :- 2^2 + 1^2 = 5.
3×3 :- 3^2 + 2^2 + 1^2 = 14.
4×4 :- 4^2 + 3^2 + 2^2 + 1^2 = 30.
Likewise, for nxn is n^2 + (n-1)^2 + (n-2)^2…………….(n-n+1)^2.
Then N(s)n = n^2 + (n-1)^2 + (n-2)^2................(n-n+1)^2 = n(n + 1)(2n + 1)/6
1.By using the formula N(s)n = n^2 + (n-1)^2 + (n-2)^2…………….(n-n+1)^2:#includeusing namespace std;
int main() { int n,squares=0;
cout<<"enter the length of a square:";
cin>>n; while(n>0) {
squares += pow(n, 2);
cout<<"no of squares in a square grid:"<n--; }
return 0; }
Output:enter the length of a square:5
no of squares in a square grid:55
2.By using the formula N(s)n =n(n + 1)(2n + 1)/6:
#includeusing namespace std;
int squares(int n) {
return n * (n + 1) * (2 * n + 1) / 6;
} int main() { int m;
cout<<"enter length of a square:";
cin>>m;
cout <<"no of squares in a square grid:"<< squares(m);
return 0; }
Output:enter length of a square:10
no of squares in a square grid:385
Similarly, you can find the number of squares inside a given square grid of any length by using these programs.
Thus, I hope this tutorial will help you.
Guys, does anyone know the answer?