Cuadrado más pequeño formado con rectángulos dados

Dado un rectángulo de largo l y ancho b , necesitamos encontrar el área del cuadrado más pequeño que se puede formar con los rectángulos de estas dimensiones dadas. 
Ejemplos: 
 

Input : 1 2
Output : 4
We can form a 2 x 2 square
using two rectangles of size
1 x 2.

Input : 7 10
Output :4900

Digamos que queremos hacer un cuadrado de longitud de lado a a partir de rectángulos de longitud l y b . Esto significa que a es un múltiplo de l & b . Como queremos el cuadrado más pequeño, tiene que ser el mínimo común múltiplo (MCM) de l & b .
Programa 1
 

C++

// C++ Program to find the area
// of the smallest square which
// can be formed with rectangles
// of given dimensions
#include <bits/stdc++.h>
using namespace std;
// Recursive function to return gcd of a and b
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 the area
// of the smallest square
int squarearea(int l, int b)
{
 
    // the length or breadth or side
    // cannot be negative
    if (l < 0 || b < 0)
        return -1;
 
 
        // LCM of length and breadth
        int n = (l * b) / gcd(l, b);
 
        // squaring to get the area
        return n * n;
     
}
 
// Driver code
int main()
{
    int l = 6, b = 4;
    cout << squarearea(l, b) << endl;
    return 0;
}

Java

// JavaProgram to find the area
// of the smallest square which
// can be formed with rectangles
// of given dimensions
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 the area
// of the smallest square
static int squarearea(int l, int b)
{
 
// the length or breadth or side
// cannot be negative
if (l < 0 || b < 0)
    return -1;
 
 
    // LCM of length and breadth
    int n = (l * b) / gcd(l, b);
 
    // squaring to get the area
    return n * n;
 
}
 
// Driver code
public static void main(String[] args)
{
    int l = 6, b = 4;
    System.out.println(squarearea(l, b));
}
}
 
// This code is contributed
// by ChitraNayal

Python 3

# Python3 Program to find the area
# of the smallest square which
# can be formed with rectangles
# of given dimensions
 
# 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 the area
# of the smallest square
def squarearea( l, b):
 
 
    # the length or breadth or side
    # cannot be negative
    if (l < 0 or b < 0):
        return -1
 
 
        # LCM of length and breadth
    n = (l * b) / gcd(l, b)
 
        # squaring to get the area
    return n * n
     
 
 
# Driver code
if __name__=='__main__':
    l = 6
    b = 4
    print(int(squarearea(l, b)))
 
#This code is contributed by ash264

C#

// C# Program to find the area
// of the smallest square which
// can be formed with rectangles
// of given dimensions
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 the area
// of the smallest square
static int squarearea(int l, int b)
{
 
// the length or breadth or side
// cannot be negative
if (l < 0 || b < 0)
    return -1;
 
 
    // LCM of length and breadth
    int n = (l * b) / gcd(l, b);
 
    // squaring to get the area
    return n * n;
 
}
 
// Driver code
public static void Main()
{
    int l = 6, b = 4;
    Console.Write(squarearea(l, b));
}
}
 
// This code is contributed
// by ChitraNayal

PHP

<?php
// PHP Program to find the area
// of the smallest square which
// can be formed with rectangles
// of given dimensions
 
// Recursive function to
// return gcd of a and b
function gcd($a, $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 the area
// of the smallest square
function squarearea($l, $b)
{
 
    // the length or breadth or side
    // cannot be negative
    if ($l < 0 || $b < 0)
        return -1;
 
 
        // LCM of length and breadth
        $n = ($l * $b) / gcd($l, $b);
 
        // squaring to get the area
        return $n * $n;
     
}
 
// Driver code
$l = 6;
$b = 4;
echo squarearea($l, $b)."\n";
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
 
// Javascript Program to find the area
// of the smallest square which
// can be formed with rectangles
// of given dimensions
 
// Recursive function to
// return gcd of a and b
function gcd(a , 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 the area
// of the smallest square
function squarearea(l , b)
{
 
// the length or breadth or side
// cannot be negative
if (l < 0 || b < 0)
    return -1;
 
 
    // LCM of length and breadth
    var n = (l * b) / gcd(l, b);
 
    // squaring to get the area
    return n * n;
 
}
 
// Driver code
 
    var l = 6, b = 4;
    document.write(squarearea(l, b));
 
// This code is contributed by Amit Katiyar
 
</script>
Producción: 

144

 

Complejidad del tiempo: O(log(min(l,b)))

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *