Find minimum perimeter for the rectangle with target area Algo

By   Tewodros   Date Posted: Oct. 13, 2021  Hits: 1,016   Category:  Algorithm   Total Comment: 0             A+ A-


side

Given an area, A for a rectangle, find the minimum perimeter for the rectangle.

Example

If Area = 60, we can think of 5 rectangles with area 60.      

1 * 60 = 60.  Perimeter = 2* ( 1 + 60) = 122

2 * 30 = 60. Perimeter = 2* ( 2 30 ) =  64

3 * 20  = 60. Perimeter = 2* ( 3 20 ) =  46

4  * 15 = 60. Perimeter = 2* ( 4   15 ) = 38

6 * 10 =  60. Perimeter = 2* ( 6 10 ) =  32

So the minimum perimeter is 32.

Solution:

Steps:

  1. we can start with the obvious and keep finding the smallest perimeter.  1 * Area = Area. So, P = 2* ( 1 + A)
  2. We can start from i=2 and keep checking if A% i == 0, then take A/i, in our case these are the 30,20,15, and 10, which we get when we divide Area by i, where i > 1 and i * i < A
  3. The last step is to compare if the new perimeter we find is less than what we already have in step 1 and if it is save the new perimeter 

 

              int min = 2*A + 2; int i = 2; int x = 1;

               while(i * i <= A)   {

                   if(A % i == 0)  {

                       x = A / i;

                       if( ((2*i) + (2*x))  < min)    {

                           min = (2 * i) + (2 * x);

                       }

                   }

                   i++;

               }

               return min;

 

 


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* mdlfmf

Back to Top