Given a paper of size A x B. Task is to cut the paper into squares of any size. Find the minimum number of squares that can be cut from the paper.

**Examples:**

Input : 13 x 29 Output : 9 Explanation : 2 (squares of size 13x13) + 4 (squares of size 3x3) + 3 (squares of size 1x1)=9 Input : 4 x 5 Output : 5 Explanation : 1 (squares of size 4x4) + 4 (squares of size 1x1)

We know that if we want to cut minimum number of squares from the paper then we would have to cut largest square possible from the paper first and largest square will have same side as smaller side of the paper. For example if paper have the size 13 x 29, then maximum square will be of side 13. so we can cut 2 square of size 13 x 13 (29/13 = 2). Now remaining paper will have size 3 x 13. Similarly we can cut remaining paper by using 4 squares of size 3 x 3 and 3 squares of 1 x 1. So minimum 9 squares can be cut from the Paper of size 13 x 29.

Below is the implementation of above approach.

## C++

`// C++ program to find minimum number of squares ` `// to cut a paper. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns min number of squares needed ` `int` `minimumSquare(` `int` `a, ` `int` `b) ` `{ ` ` ` `long` `long` `result = 0, rem = 0; ` ` ` ` ` `// swap if a is small size side . ` ` ` `if` `(a < b) ` ` ` `swap(a, b); ` ` ` ` ` `// Iterate until small size side is ` ` ` `// greater then 0 ` ` ` `while` `(b > 0) ` ` ` `{ ` ` ` `// Update result ` ` ` `result += a/b; ` ` ` ` ` `long` `long` `rem = a % b; ` ` ` `a = b; ` ` ` `b = rem; ` ` ` `} ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 13, m = 29; ` ` ` `cout << minimumSquare(n, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum ` `// number of squares to cut a paper. ` `class` `GFG{ ` ` ` `// To swap two numbers ` `static` `void` `swap(` `int` `a,` `int` `b) ` `{ ` ` ` `int` `temp = a; ` ` ` `a = b; ` ` ` `b = temp; ` `} ` ` ` `// Returns min number of squares needed ` `static` `int` `minimumSquare(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `result = ` `0` `, rem = ` `0` `; ` ` ` ` ` `// swap if a is small size side . ` ` ` `if` `(a < b) ` ` ` `swap(a, b); ` ` ` ` ` `// Iterate until small size side is ` ` ` `// greater then 0 ` ` ` `while` `(b > ` `0` `) ` ` ` `{ ` ` ` `// Update result ` ` ` `result += a/b; ` ` ` `rem = a % b; ` ` ` `a = b; ` ` ` `b = rem; ` ` ` `} ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `13` `, m = ` `29` `; ` ` ` `System.out.println(minimumSquare(n, m)); ` `} ` `} ` ` ` `//This code is contributed by Smitha Dinesh Semwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find minimum ` `# number of squares to cut a paper. ` ` ` `# Returns min number of squares needed ` `def` `minimumSquare(a, b): ` ` ` ` ` `result ` `=` `0` ` ` `rem ` `=` `0` ` ` ` ` `# swap if a is small size side . ` ` ` `if` `(a < b): ` ` ` `a, b ` `=` `b, a ` ` ` ` ` `# Iterate until small size side is ` ` ` `# greater then 0 ` ` ` `while` `(b > ` `0` `): ` ` ` ` ` `# Update result ` ` ` `result ` `+` `=` `int` `(a ` `/` `b) ` ` ` ` ` `rem ` `=` `int` `(a ` `%` `b) ` ` ` `a ` `=` `b ` ` ` `b ` `=` `rem ` ` ` ` ` `return` `result ` ` ` `# Driver code ` `n ` `=` `13` `m ` `=` `29` ` ` `print` `(minimumSquare(n, m)) ` ` ` `# This code is contributed by ` `# Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// number of squares to cut a paper. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// To swap two numbers ` `static` `void` `swap(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `temp = a; ` ` ` `a = b; ` ` ` `b = temp; ` `} ` ` ` `// Returns min number of squares needed ` `static` `int` `minimumSquare(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `result = 0, rem = 0; ` ` ` ` ` `// swap if a is small size side . ` ` ` `if` `(a < b) ` ` ` `swap(a, b); ` ` ` ` ` `// Iterate until small size side is ` ` ` `// greater then 0 ` ` ` `while` `(b > 0) ` ` ` `{ ` ` ` `// Update result ` ` ` `result += a / b; ` ` ` `rem = a % b; ` ` ` `a = b; ` ` ` `b = rem; ` ` ` `} ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 13, m = 29; ` ` ` `Console.WriteLine(minimumSquare(n, m)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

9

**Note that the above Greedy solution doesn’t always produce optimal result**. For example if input is 36 x 30, the above algorithm would produce output 6, but we can cut the paper in 5 squares

1) Three squares of size 12 x 12

2) Two squares of size 18 x 18.

Thanks to Sergey V. Pereslavtsev for pointing out the above case.

This article is contributed by **Kuldeep Singh(kulli_d_coder)**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

## Recommended Posts:

- Minimum Cost to cut a board into squares
- Minimum squares to evenly cut a rectangle
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Number of ways to cut a stick of length N into K pieces
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Minimum number of squares whose sum equals to given number N | set 2
- Minimum number of squares whose sum equals to a given number N | Set-3
- Minimum number of squares whose sum equals to given number n
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Puzzle | Dividing a Square into N smaller squares
- Pizza cut problem (Or Circle Division by Lines)
- Maximum of smallest possible area that can get with exactly k cut of given rectangular
- Puzzle | Cut Blue Painted Cube
- Find if there exists multiple ways to draw line through (x, y) to cut rectangle in equal halfs
- Minimum squares to cover a rectangle
- Number of ways to distribute N Paper Set among M students
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Count number of squares in a rectangle
- Number of perfect squares between two given numbers