In the previous article, we have discussed Python Program To Find Area of a Circular Sector
Given n blocks of size 1*1, the task is to find the minimum perimeter of the grid made by these given n blocks in python.
Examples:
Example1:
Input:
Given n value = 6
Output:
The minimum perimeter of the grid made by the given n blocks{ 6 } = 11
Example2:
Input:
Given n value = 9
Output:
The minimum perimeter of the grid made by the given n blocks{ 9 } = 12
Program for Minimum Perimeter of n Blocks in Python
Below are the ways to find the minimum perimeter of the grid made by these given n blocks in python:
Method #1: Using Mathematical Approach (Static Input)
Approach:
- Import math module using the import keyword.
- Give the n value as static input and store it in a variable.
- Create a function to say Minimum_perimtr() which takes the given n value as an argument and returns the minimum perimeter of the grid made by these given n blocks.
- Calculate the square root of the given n value using the math.sqrt() function and store it in a variable say sqrt_val.
- Multiply the above result with itself and store it in another variable.
- Check if the given n value is a perfect square by using the if conditional statement.
- If it is true, then return the value of above sqrt_val multiplied by 4.
- Else calculate the number of rows by dividing the given n value by sqrt_val.
- Add the above sqrt_val with the number of rows obtained and multiply the result by 2 to get the perimeter of the rectangular grid.
- Store it in another variable.
- Check whether there are any blocks left using the if conditional statement.
- If it is true, then add 2 to the above-obtained perimeter of the rectangular grid and store it in the same variable.
- Return the minimum perimeter of the grid made by the given n blocks.
- Pass the given n value as an argument to the Minimum_perimtr()Â function, convert it into an integer using the int() function and store it in another variable.
- Print the above result which is the minimum perimeter of the grid made by the given n blocks.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword. import math # Create a function to say Minimum_perimtr() which takes the given n value as an # argument and returns the minimum perimeter of the grid made by these given n blocks. def Minimum_perimtr(gvn_n_val): # Calculate the square root of the given n value using the math.sqrt() function # and store it in a variable say sqrt_val. sqrt_val = math.sqrt(gvn_n_val) # Multiply the above result with itself and store it in another variable. sqre_rslt = sqrt_val * sqrt_val # Check if the given n value is a perfect square by using the if # conditional statement. if (sqre_rslt == gvn_n_val): # If it is true, then return the value of above sqrt_val multiplied by 4. return sqrt_val * 4 else: # Else calculate the number of rows by dividing the given n value by sqrt_val. no_of_rows = gvn_n_val / sqrt_val # Add the above sqrt_val with the number of rows obtained and multiply the result # by 2 to get the perimeter of the rectangular grid. # Store it in another variable. rslt_perimetr = 2 * (sqrt_val + no_of_rows) # Check whether there are any blocks left using the if conditional statement. if (gvn_n_val % sqrt_val != 0): # If it is true, then add 2 to the above-obtained perimeter of the rectangular # grid and store it in the same variable. rslt_perimetr += 2 # Return the minimum perimeter of the grid made by the given n blocks. return rslt_perimetr # Give the n value as static input and store it in a variable. gvn_n_val = 6 # Pass the given n value as an argument to the Minimum_perimtr()Â function, convert # it into an integer using the int() function and store it in another variable. fnl_rslt = int(Minimum_perimtr(gvn_n_val)) # Print the above result which is the minimum perimeter of the grid made by the # given n blocks. print( "The minimum perimeter of the grid made by the given n blocks{", gvn_n_val, "} = ", fnl_rslt)
#include <iostream> #include<math.h> using namespace std; int Minimum_perimtr ( int gvn_n_val ) { int sqrt_val = sqrt ( gvn_n_val ); int sqre_rslt = sqrt_val * sqrt_val; if ( sqre_rslt == gvn_n_val ) { return sqrt_val * 4; } else { int no_of_rows = gvn_n_val / sqrt_val; int rslt_perimetr = 2 * ( sqrt_val + no_of_rows ); if ( ( gvn_n_val % sqrt_val != 0 ) ) { rslt_perimetr += 2; } return rslt_perimetr; } } int main() { int gvn_n_val = 6; int fnl_rslt = ( int ) Minimum_perimtr ( gvn_n_val ); cout << "The minimum perimeter of the grid made by the given n blocks{" << gvn_n_val << "} = " << fnl_rslt << endl; return 0; }
Output:
The minimum perimeter of the grid made by the given n blocks{ 6 } = 11
Method #2: Using Mathematical Approach (User Input)
Approach:
- Import math module using the import keyword.
- Give the n value as user input using the int(input()) function input and store it in a variable.
- Create a function to say Minimum_perimtr() which takes the given n value as an argument and returns the minimum perimeter of the grid made by these given n blocks.
- Calculate the square root of the given n value using the math.sqrt() function and store it in a variable say sqrt_val.
- Multiply the above result with itself and store it in another variable.
- Check if the given n value is a perfect square by using the if conditional statement.
- If it is true, then return the value of above sqrt_val multiplied by 4.
- Else calculate the number of rows by dividing the given n value by sqrt_val.
- Add the above sqrt_val with the number of rows obtained and multiply the result by 2 to get the perimeter of the rectangular grid.
- Store it in another variable.
- Check whether there are any blocks left using the if conditional statement.
- If it is true, then add 2 to the above-obtained perimeter of the rectangular grid and store it in the same variable.
- Return the minimum perimeter of the grid made by the given n blocks.
- Pass the given n value as an argument to the Minimum_perimtr()Â function, convert it into an integer using the int() function and store it in another variable.
- Print the above result which is the minimum perimeter of the grid made by the given n blocks.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword. import math # Create a function to say Minimum_perimtr() which takes the given n value as an # argument and returns the minimum perimeter of the grid made by these given n blocks. def Minimum_perimtr(gvn_n_val): # Calculate the square root of the given n value using the math.sqrt() function # and store it in a variable say sqrt_val. sqrt_val = math.sqrt(gvn_n_val) # Multiply the above result with itself and store it in another variable. sqre_rslt = sqrt_val * sqrt_val # Check if the given n value is a perfect square by using the if # conditional statement. if (sqre_rslt == gvn_n_val): # If it is true, then return the value of above sqrt_val multiplied by 4. return sqrt_val * 4 else: # Else calculate the number of rows by dividing the given n value by sqrt_val. no_of_rows = gvn_n_val / sqrt_val # Add the above sqrt_val with the number of rows obtained and multiply the result # by 2 to get the perimeter of the rectangular grid. # Store it in another variable. rslt_perimetr = 2 * (sqrt_val + no_of_rows) # Check whether there are any blocks left using the if conditional statement. if (gvn_n_val % sqrt_val != 0): # If it is true, then add 2 to the above-obtained perimeter of the rectangular # grid and store it in the same variable. rslt_perimetr += 2 # Return the minimum perimeter of the grid made by the given n blocks. return rslt_perimetr # Give the n value as user input using the int(input()) function input and # store it in a variable. gvn_n_val = int(input("Enter some random number = ")) # Pass the given n value as an argument to the Minimum_perimtr()Â function, convert # it into an integer using the int() function and store it in another variable. fnl_rslt = int(Minimum_perimtr(gvn_n_val)) # Print the above result which is the minimum perimeter of the grid made by the # given n blocks. print( "The minimum perimeter of the grid made by the given n blocks{", gvn_n_val, "} = ", fnl_rslt)
Output:
Enter some random number = 9 The minimum perimeter of the grid made by the given n blocks{ 9 } = 12
If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.