Rectángulo con la mínima diferencia posible entre el largo y el ancho

Dada un área entera , la tarea es encontrar el largo y el ancho de un rectángulo con el área dada tal que la diferencia entre el largo y el ancho sea la mínima posible.

Ejemplos:  

Entrada: área = 99 
Salida: l = 11, b = 9 
Todos los rectángulos posibles (l, b) son (99, 1), (33, 3) y (11, 9) 
Y el que tiene el mínimo |l – b | es (11, 9)

Entrada: área = 25 
Salida: l = 5, b = 5 

Enfoque: La tarea es encontrar dos números enteros l y b tales que l * b = área y |l – b| son los mínimos posibles. La factorización se puede usar para resolver el problema, pero hacer solo una factorización simple de 1 a N llevará mucho tiempo obtener el resultado requerido para valores más grandes de  N.
Para superar esto, simplemente iterar hasta. Considerando            < l ≤ N , entonces para todos los valores de l , b siempre será           .

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the length (l)
// and breadth (b) of the rectangle
// having area = N and |l - b| as
// minimum as possible
void find_rectangle(int area)
{
    int l, b;
    int M = sqrt(area), ans;
 
    for (int i = M; i >= 1; i--) {
 
        // i is a factor
        if (area % i == 0) {
 
            // l >= sqrt(area) >= i
            l = (area / i);
 
            // so here l is +ve always
            b = i;
            break;
        }
    }
 
    // Here l and b are length and
    // breadth of the rectangle
    cout << "l = " << l << ", b = "
         << b << endl;
}
 
// Driver code
int main()
{
    int area = 99;
    find_rectangle(area);
    return 0;
}

Java

// Java implementation of the approach
class GFG {
 
    // Function to print the length (l)
    // and breadth (b) of the rectangle
    // having area = N and |l - b| as
    // minimum as possible
    static void find_rectangle(int area)
    {
        int l = 0, b = 0;
        int M = (int)Math.sqrt(area), ans;
 
        for (int i = M; i >= 1; i--) {
 
            // i is a factor
            if (area % i == 0) {
 
                // l >= sqrt(area) >= i
                l = (area / i);
 
                // so here l is +ve always
                b = i;
                break;
            }
        }
 
        // Here l and b are length and
        // breadth of the rectangle
        System.out.println("l = " + l + ", b = " + b);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int area = 99;
        find_rectangle(area);
    }
}
 
// This code is contributed by Ita_c.

Python3

# Python3 implementation of the approach
import math as mt
 
# Function to print the length (l)
# and breadth (b) of the rectangle
# having area = N and |l - b| as
# minimum as possible
def find_rectangle(area):
 
    l, b = 0, 0
    M = mt.ceil(mt.sqrt(area))
    ans = 0
 
    for i in range(M, 0, -1):
 
        # i is a factor
        if (area % i == 0):
 
            # l >= sqrt(area) >= i
            l = (area // i)
 
            # so here l is + ve always
            b = i
            break
         
    # Here l and b are length and
    # breadth of the rectangle
    print("l =", l, ", b =", b)
 
# Driver code
area = 99
find_rectangle(area)
 
# This code is contributed by
# Mohit kumar 29

C#

// C# implementation of the approach
using System;
class GFG {
 
    // Function to print the length (l)
    // and breadth (b) of the rectangle
    // having area = N and |l - b| as
    // minimum as possible
    static void find_rectangle(int area)
    {
        int l = 0, b = 0;
        int M = (int)Math.Sqrt(area);
 
        for (int i = M; i >= 1; i--) {
 
            // i is a factor
            if (area % i == 0) {
 
                // l >= sqrt(area) >= i
                l = (area / i);
 
                // so here l is +ve always
                b = i;
                break;
            }
        }
 
        // Here l and b are length and
        // breadth of the rectangle
        Console.WriteLine("l = " + l + ", b = " + b);
    }
 
    // Driver code
    public static void Main()
    {
        int area = 99;
        find_rectangle(area);
    }
}
 
// This code is contributed by Mukul Singh.

PHP

<?php
// Php implementation of the approach
 
// Function to print the length (l)
// and breadth (b) of the rectangle
// having area = N and |l - b| as
// minimum as possible
function find_rectangle($area)
{
    $M = floor(sqrt($area));
 
    for ($i = $M; $i >= 1; $i--)
    {
 
        // i is a factor
        if ($area % $i == 0)
        {
 
            // l >= sqrt(area) >= i
            $l = floor($area / $i);
 
            // so here l is +ve always
            $b = $i ;
            break;
        }
    }
 
    // Here l and b are length and
    // breadth of the rectangle
    echo "l = ", $l, ", b = ", $b, "\n";
}
 
// Driver code
$area = 99;
find_rectangle($area);
 
// This code is contributed by Ryuga
?>

Javascript

<script>
 
// Javascript implementation of the approach
 
    // Function to print the length (l)
    // and breadth (b) of the rectangle
    // having area = N and |l - b| as
    // minimum as possible
    function find_rectangle(area)
    {
        let l = 0, b = 0;
        let M = Math.floor(Math.sqrt(area)), ans;
 
        for (let i = M; i >= 1; i--) {
 
            // i is a factor
            if (area % i == 0) {
 
                // l >= sqrt(area) >= i
                l = Math.floor(area / i);
 
                // so here l is +ve always
                b = i;
                break;
            }
        }
 
        // Here l and b are length and
        // breadth of the rectangle
        document.write("l = " + l + ", b = " + b);
    }
 
// Driver code
 
    let area = 99;
    find_rectangle(area);
     
</script>
Producción: 

l = 11, b = 9

 

Complejidad del Tiempo: O( \sqrt{N})
 

A continuación se muestra una implementación simple.  

CPP

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the length (l)
// and breadth (b) of the rectangle
// having area = N and |l - b| as
// minimum as possible
void find_rectangle(int area)
{
    for (int i = ceil(sqrt(area)); i <= area; i++) {
        if (area / i * i == area) {
            printf("%d %d", i, area / i);
            return;
        }
    }
}
 
// Driver code
int main()
{
    int area = 99;
    find_rectangle(area);
    return 0;
}

Java

// Java implementation of the approach
import java.io.*;
class GFG {
    // Function to print the length (l)
    // and breadth (b) of the rectangle
    // having area = N and |l - b| as
    // minimum as possible
    static void find_rectangle(int area)
    {
        for(int i = (int)Math.ceil(Math.sqrt(area)); i <= area; i++)
        {
            if(area / i * i == area)
            {
                System.out.println(i + " " + (int)(area / i));
                return;
            }
        }
    }
   
    // Driver code
    public static void main (String[] args)
    {
        int area = 99;
        find_rectangle(area);       
    }
}
 
// This code is contributed by rag2127

Python3

# Python3 implementation of the approach
import math
 
# Function to print the length (l)
# and breadth (b) of the rectangle
# having area = N and |l - b| as
# minimum as possible
def find_rectangle(area):
    for i in range(int(math.ceil(math.sqrt(area))) , area + 1):
        if((int(area / i) * i) == area):
            print(i, int(area / i))
            return
 
# Driver code
area = 99
find_rectangle(area)
 
# This code is contributed by avanitrachhadiya2155

C#

// C# implementation of the approach
using System;
class GFG
{
 
  // Function to print the length (l)
  // and breadth (b) of the rectangle
  // having area = N and |l - b| as
  // minimum as possible
  static void find_rectangle(int area)
  {
    for(int i = (int)Math.Ceiling(Math.Sqrt(area)); i <= area; i++)
    {
      if(area / i * i == area)
      {
        Console.WriteLine(i + " " + (int)(area / i));
        return;
      }
    }
  }
 
  // Driver code
  static void Main()
  {
    int area = 99;
    find_rectangle(area);
  }
}
 
// This code is contributed by divyeshrabadiya07.

Javascript

<script>
// Javascript implementation of the approach
 
// Function to print the length (l)
// and breadth (b) of the rectangle
// having area = N and |l - b| as
// minimum as possible
function find_rectangle(are)
{
    for(let i = Math.floor(Math.ceil(Math.sqrt(area))); i <= area; i++)
        {
            if(Math.floor(area / i) * i == area)
            {
                document.write(i + " " + Math.floor(area / i));
                return;
            }
        }
}
 
 // Driver code
let area = 99;
find_rectangle(area);  
 
 
 
// This code is contributed by unknown2108
</script>
Producción: 

11 9

 

Publicación traducida automáticamente

Artículo escrito por Lohith sai Andra 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 *