# Number of squares of maximum area in a rectangle

Given a rectangle of sides m and n. Cut the rectangle into smaller identical pieces such that each piece is a square having maximum possible side length with no leftover part of the rectangle. Print number of such squares formed.

Examples:

```Input: 9 6
Output: 6
Rectangle can be cut into squares of size 3.

Input: 4 2
Output: 2
Rectangle can be cut into squares of size 2.
```

Approach: The task is to cut the rectangle in squares with the side of length s without pieces of the rectangle left over, so s must divide both m and n. Also, the side of the square should be maximum possible, therefore, s should be the greatest common divisor of m and n.
so, s = gcd(m, n).
To find the number of squares the rectangle is cut into, the task to be done is to divide the area of a rectangle with an area of the square of size s.

## C++

 `// C++ code for calculating the ` `// number of squares ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find number of squares ` `int` `NumberOfSquares(``int` `x, ``int` `y) ` `{ ` `    ``// Here in built c++ gcd function is used ` `    ``int` `s = __gcd(x, y); ` ` `  `    ``int` `ans = (x * y) / (s * s); ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `m = 385, n = 60; ` ` `  `    ``// Call the function NumberOfSquares ` `    ``cout << NumberOfSquares(m, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java code for calculating  ` `// the number of squares ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `    ``// Recursive function to ` `    ``// return gcd of a and b ` `    ``static` `int` `__gcd(``int` `a, ``int` `b) ` `    ``{ ` `        ``// Everything divides 0  ` `        ``if` `(a == ``0` `|| b == ``0``) ` `        ``return` `0``; ` `     `  `        ``// base case ` `        ``if` `(a == b) ` `            ``return` `a; ` `     `  `        ``// a is greater ` `        ``if` `(a > b) ` `            ``return` `__gcd(a - b, b); ` `        ``return` `__gcd(a, b - a); ` `    ``}  ` ` `  ` `  `// Function to find  ` `// number of squares ` `static` `int` `NumberOfSquares(``int` `x,  ` `                           ``int` `y) ` `{ ` `    ``// Here in built c++  ` `    ``// gcd function is used ` `    ``int` `s = __gcd(x, y); ` ` `  `    ``int` `ans = (x * y) / (s * s); ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `m = ``385``, n = ``60``; ` ` `  `    ``// Call the function ` `    ``// NumberOfSquares ` `    ``System.out.println(NumberOfSquares(m, n)); ` `} ` `} ` ` `  `// This code is contributed by anuj_67. `

## Python3

 `# Python3 code for calculating  ` `# the number of squares ` ` `  `# Recursive function to ` `# return gcd of a and b ` `def` `__gcd(a, b): ` `     `  `    ``# Everything divides 0  ` `    ``if` `(a ``=``=` `0` `or` `b ``=``=` `0``): ` `        ``return` `0``; ` ` `  `    ``# base case ` `    ``if` `(a ``=``=` `b): ` `        ``return` `a; ` ` `  `    ``# a is greater ` `    ``if` `(a > b): ` `        ``return` `__gcd(a ``-` `b, b); ` `    ``return` `__gcd(a, b ``-` `a); ` ` `  `# Function to find  ` `# number of squares ` `def` `NumberOfSquares(x, y): ` `     `  `    ``# Here in built PHP ` `    ``# gcd function is used ` `    ``s ``=` `__gcd(x, y); ` ` `  `    ``ans ``=` `(x ``*` `y) ``/` `(s ``*` `s); ` ` `  `    ``return` `int``(ans); ` ` `  `# Driver Code ` `m ``=` `385``; ` `n ``=` `60``; ` ` `  `# Call the function ` `# NumberOfSquares ` `print``(NumberOfSquares(m, n)); ` ` `  `# This code is contributed  ` `# by mit `

## C#

 `// C# code for calculating  ` `// the number of squares ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Recursive function to ` `    ``// return gcd of a and b ` `    ``static` `int` `__gcd(``int` `a, ``int` `b) ` `    ``{ ` `        ``// Everything divides 0  ` `        ``if` `(a == 0 || b == 0) ` `        ``return` `0; ` `     `  `        ``// base case ` `        ``if` `(a == b) ` `            ``return` `a; ` `     `  `        ``// a is greater ` `        ``if` `(a > b) ` `            ``return` `__gcd(a - b, b); ` `        ``return` `__gcd(a, b - a); ` `    ``}  ` ` `  ` `  `// Function to find  ` `// number of squares ` `static` `int` `NumberOfSquares(``int` `x,  ` `                           ``int` `y) ` `{ ` `    ``// Here in built c++  ` `    ``// gcd function is used ` `    ``int` `s = __gcd(x, y); ` ` `  `    ``int` `ans = (x * y) /  ` `              ``(s * s); ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `int` `m = 385, n = 60; ` ` `  `// Call the function ` `// NumberOfSquares ` `Console.WriteLine(NumberOfSquares(m, n)); ` `} ` `} ` ` `  `// This code is contributed by ajit `

## PHP

 ` ``\$b``) ` `        ``return` `__gcd(``\$a` `- ``\$b``, ``\$b``); ` `    ``return` `__gcd(``\$a``, ``\$b` `- ``\$a``); ` `}  ` ` `  `// Function to find  ` `// number of squares ` `function` `NumberOfSquares(``\$x``, ``\$y``)  ` `{ ` `    ``// Here in built PHP ` `    ``// gcd function is used ` `    ``\$s` `= __gcd(``\$x``, ``\$y``); ` ` `  `    ``\$ans` `= (``\$x` `* ``\$y``) /  ` `           ``(``\$s` `* ``\$s``); ` ` `  `    ``return` `\$ans``; ` `} ` ` `  `// Driver Code ` `\$m` `= 385; ` `\$n` `= 60; ` ` `  `// Call the function ` `// NumberOfSquares ` `echo` `(NumberOfSquares(``\$m``, ``\$n``)); ` ` `  `// This code is contributed  ` `// by akt_mit ` `?> `

Output:

```924
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m, jit_t, Mithun Kumar

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.