Given an integer named area which represents the area of a rectangle, write a function minPerimeter that calculates the minimum possible perimeter of the given rectangle, and prints the perimeter and the side lengths needed to achieve that minimum. The length of the sides of the rectangle are integer numbers.
For example, given the integer area = 30, possible perimeters and side-lengths are:
(1, 30), with a perimeter of 62 (2, 15), with a perimeter of 34 (3, 10), with a perimeter of 26 (5, 6), with a perimeter of 22
Thus, your function should print only the last line, since that is the minimum perimeter.
A python program
The below code that helps give you the desired result and it will give you the result of a rectangle only(i.e a and b are not equal )
import math;
def minperimeter(n):
a,b,p,m=0,0,0,1000000;
for i in range(1,math.floor(math.sqrt(n))):
if n%i==0:
p=2*(i+(n//i));
if p<m:
m=p;
a=i;
b=n//i;
print("(%d,%d),with a perimeter of %d"%(a,b,m));
n=int(input());
minperimeter(n);
*********************************************************************************************************************************
The below code will give the results of rectangles which also includes square(i.e rectangle which all sides are equal)
import math;
def minperimeter(n):
a,b,p,m=0,0,0,1000000;
for i in range(1,math.floor(math.sqrt(n))+1):
if n%i==0:
p=2*(i+(n//i));
if p<m:
m=p;
a=i;
b=n//i;
print("(%d,%d),with a perimeter of %d"%(a,b,m));
n=int(input());
minperimeter(n);
Both the programs have only a minor change that is +1 in the second program of range function in for loop.
Get Answers For Free
Most questions answered within 1 hours.