Paper Cut into Minimum Number of Squares

22 Jun, 2022
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 a minimum number of squares from the paper then we would have to cut the largest square possible from the paper first and the largest square will have the same side as the smaller side of the paper. For example, if the paper has the size 13 x 29, then the 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 the remaining paper by using 4 squares of size 3 x 3 and 3 squares of 1 x 1. So a minimum of 9 squares can be cut from the Paper of size 13 x 29. 
Below is the implementation of the above approach.  
C++
Java
Python3
C#
Javascript
// C++ program to find minimum number of squares// to cut a paper.#include<bits/stdc++.h>using namespace std; // Returns min number of squares neededint 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 codeint main(){    int n = 13, m = 29;    cout << minimumSquare(n, m);    return 0;}
// Java program to find minimum// number of squares to cut a paper.class GFG{     // To swap two numbersstatic void swap(int a,int b){    int temp = a;    a = b;    b = temp;} // Returns min number of squares neededstatic 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 codepublic static void main(String[] args){    int n = 13, m = 29;    System.out.println(minimumSquare(n, m));}} //This code is contributed by Smitha Dinesh Semwal.
# Python 3 program to find minimum# number of squares to cut a paper. # Returns min number of squares neededdef 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 coden = 13m = 29 print(minimumSquare(n, m)) # This code is contributed by# Smitha Dinesh Semwal
// C# program to find minimum// number of squares to cut a paper.using System;     class GFG{     // To swap two numbersstatic void swap(int a, int b){    int temp = a;    a = b;    b = temp;} // Returns min number of squares neededstatic 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 codepublic static void Main(String[] args){    int n = 13, m = 29;    Console.WriteLine(minimumSquare(n, m));}} // This code is contributed by 29AjayKumar
<script> // Javascript program to find// minimum number of squares// to cut a paper. // Returns min number of squares neededfunction minimumSquare(a, b){    let result = 0, rem = 0;     // swap if a is small size side .    if (a < b) {        let temp = a;        a = b;        b = temp;    }     // Iterate until small size side is    // greater then 0    while (b > 0)    {        // Update result        result += parseInt(a/b);         let rem = a % b;        a = b;        b = rem;    }     return result;} // Driver code    let n = 13, m = 29;    document.write(minimumSquare(n, m)); </script>
Output: 
9
Time Complexity: O(log(max(a,b))) Auxiliary Space: O(1)
Note that the above Greedy solution doesn’t always produce an optimal result. For example, if the input is 36 x 30, the above algorithm would produce output 6, but we can cut the paper into 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 Aarti_Rathi and Kuldeep Singh(kulli_d_coder). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
29AjayKumar
rishavmahato348
codewithrathi
Combinatorial
Geometric
Greedy
Mathematical
Greedy
Mathematical
Combinatorial
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Write a program to print all permutations of a given string
Permutation and Combination in Python
Factorial of a large number
Count of subsets with sum equal to X
Combinational Sum
Program for distance between two points on earth
Find if two rectangles overlap
Check whether triangle is valid or not if sides are given
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Program for Point of Intersection of Two Lines