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++
#include <bits/stdc++.h>
using namespace std;
int NumberOfSquares( int x, int y)
{
int s = __gcd(x, y);
int ans = (x * y) / (s * s);
return ans;
}
int main()
{
int m = 385, n = 60;
cout << NumberOfSquares(m, n);
return 0;
}
|
C
#include <stdio.h>
int gcd( int a, int b)
{
int gcd = 1;
for ( int i = 1; i <= a && i <= b; i++)
{
if (a % i ==0 && b % i == 0)
gcd = i;
}
return gcd;
}
int NumberOfSquares( int x, int y)
{
int s = gcd(x, y);
int ans = (x * y) / (s * s);
return ans;
}
int main()
{
int m = 385, n = 60;
printf ( "%d" ,NumberOfSquares(m, n));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0 || b == 0 )
return 0 ;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int NumberOfSquares( int x,
int y)
{
int s = __gcd(x, y);
int ans = (x * y) / (s * s);
return ans;
}
public static void main (String[] args)
{
int m = 385 , n = 60 ;
System.out.println(NumberOfSquares(m, n));
}
}
|
Python3
def __gcd(a, b):
if (a = = 0 or b = = 0 ):
return 0 ;
if (a = = b):
return a;
if (a > b):
return __gcd(a - b, b);
return __gcd(a, b - a);
def NumberOfSquares(x, y):
s = __gcd(x, y);
ans = (x * y) / (s * s);
return int (ans);
m = 385 ;
n = 60 ;
print (NumberOfSquares(m, n));
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int NumberOfSquares( int x,
int y)
{
int s = __gcd(x, y);
int ans = (x * y) /
(s * s);
return ans;
}
static public void Main ()
{
int m = 385, n = 60;
Console.WriteLine(NumberOfSquares(m, n));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0 || $b == 0)
return 0;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
function NumberOfSquares( $x , $y )
{
$s = __gcd( $x , $y );
$ans = ( $x * $y ) /
( $s * $s );
return $ans ;
}
$m = 385;
$n = 60;
echo (NumberOfSquares( $m , $n ));
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function NumberOfSquares(x, y)
{
let s = __gcd(x, y);
let ans = parseInt((x * y) / (s * s));
return ans;
}
let m = 385, n = 60;
document.write(NumberOfSquares(m, n));
</script>
|
Time complexity: O(log(max(m,n))
Auxiliary Space: O(1)
Last Updated :
13 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...