{"id":22961,"date":"2021-09-29T20:14:41","date_gmt":"2021-09-29T14:44:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=22961"},"modified":"2021-11-22T18:35:34","modified_gmt":"2021-11-22T13:05:34","slug":"python-program-for-minimum-perimeter-of-n-blocks","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-minimum-perimeter-of-n-blocks\/","title":{"rendered":"Python Program for Minimum Perimeter of n Blocks"},"content":{"rendered":"
In the previous article, we have discussed Python Program To Find Area of a Circular Sector<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the minimum perimeter of the grid made by these given n blocks in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If you are learning Python then the Python Programming Example<\/a> is for you and gives you a thorough description of concepts for beginners, experienced programmers.<\/p>\n 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 …<\/p>\n
\nGiven 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.<\/p>\nGiven n value = 6<\/pre>\n
The minimum perimeter of the grid made by the given n blocks{ 6 } = 11<\/pre>\n
Given n value = 9<\/pre>\n
The minimum perimeter of the grid made by the given n blocks{ 9 } = 12<\/pre>\n
Program for Minimum Perimeter of n Blocks in Python<\/h2>\n
\n
Method #1: Using Mathematical Approach (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n\r\n# Create a function to say Minimum_perimtr() which takes the given n value as an\r\n# argument and returns the minimum perimeter of the grid made by these given n blocks.\r\n\r\n\r\ndef Minimum_perimtr(gvn_n_val):\r\n # Calculate the square root of the given n value using the math.sqrt() function\r\n # and store it in a variable say sqrt_val.\r\n sqrt_val = math.sqrt(gvn_n_val)\r\n # Multiply the above result with itself and store it in another variable.\r\n sqre_rslt = sqrt_val * sqrt_val\r\n\r\n # Check if the given n value is a perfect square by using the if\r\n # conditional statement.\r\n if (sqre_rslt == gvn_n_val):\r\n # If it is true, then return the value of above sqrt_val multiplied by 4.\r\n return sqrt_val * 4\r\n else:\r\n # Else calculate the number of rows by dividing the given n value by sqrt_val.\r\n no_of_rows = gvn_n_val \/ sqrt_val\r\n\r\n # Add the above sqrt_val with the number of rows obtained and multiply the result\r\n # by 2 to get the perimeter of the rectangular grid.\r\n # Store it in another variable.\r\n rslt_perimetr = 2 * (sqrt_val + no_of_rows)\r\n\r\n # Check whether there are any blocks left using the if conditional statement.\r\n if (gvn_n_val % sqrt_val != 0):\r\n # If it is true, then add 2 to the above-obtained perimeter of the rectangular\r\n # grid and store it in the same variable.\r\n rslt_perimetr += 2\r\n # Return the minimum perimeter of the grid made by the given n blocks.\r\n return rslt_perimetr\r\n\r\n\r\n# Give the n value as static input and store it in a variable.\r\ngvn_n_val = 6\r\n# Pass the given n value as an argument to the Minimum_perimtr()\u00a0function, convert\r\n# it into an integer using the int() function and store it in another variable.\r\nfnl_rslt = int(Minimum_perimtr(gvn_n_val))\r\n# Print the above result which is the minimum perimeter of the grid made by the\r\n# given n blocks.\r\nprint(\r\n \"The minimum perimeter of the grid made by the given n blocks{\", gvn_n_val, \"} = \", fnl_rslt)\r\n<\/pre>\n
#include <iostream>\r\n#include<math.h>\r\nusing namespace std;\r\nint Minimum_perimtr ( int gvn_n_val ) {\r\n int sqrt_val = sqrt ( gvn_n_val );\r\n int sqre_rslt = sqrt_val * sqrt_val;\r\n if ( sqre_rslt == gvn_n_val ) {\r\n return sqrt_val * 4;\r\n }\r\n else {\r\n int no_of_rows = gvn_n_val \/ sqrt_val;\r\n int rslt_perimetr = 2 * ( sqrt_val + no_of_rows );\r\n if ( ( gvn_n_val % sqrt_val != 0 ) ) {\r\n rslt_perimetr += 2;\r\n }\r\n return rslt_perimetr;\r\n }\r\n}\r\nint main() {\r\n int gvn_n_val = 6;\r\n int fnl_rslt = ( int ) Minimum_perimtr ( gvn_n_val );\r\n cout << \"The minimum perimeter of the grid made by the given n blocks{\" << gvn_n_val << \"} = \" << fnl_rslt << endl;\r\n return 0;\r\n}\r\n\r\n\r\n\r\n<\/pre>\n
The minimum perimeter of the grid made by the given n blocks{ 6 } = 11<\/pre>\n
Method #2: Using Mathematical Approach (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n\r\n# Create a function to say Minimum_perimtr() which takes the given n value as an\r\n# argument and returns the minimum perimeter of the grid made by these given n blocks.\r\n\r\n\r\ndef Minimum_perimtr(gvn_n_val):\r\n # Calculate the square root of the given n value using the math.sqrt() function\r\n # and store it in a variable say sqrt_val.\r\n sqrt_val = math.sqrt(gvn_n_val)\r\n # Multiply the above result with itself and store it in another variable.\r\n sqre_rslt = sqrt_val * sqrt_val\r\n\r\n # Check if the given n value is a perfect square by using the if\r\n # conditional statement.\r\n if (sqre_rslt == gvn_n_val):\r\n # If it is true, then return the value of above sqrt_val multiplied by 4.\r\n return sqrt_val * 4\r\n else:\r\n # Else calculate the number of rows by dividing the given n value by sqrt_val.\r\n no_of_rows = gvn_n_val \/ sqrt_val\r\n\r\n # Add the above sqrt_val with the number of rows obtained and multiply the result\r\n # by 2 to get the perimeter of the rectangular grid.\r\n # Store it in another variable.\r\n rslt_perimetr = 2 * (sqrt_val + no_of_rows)\r\n\r\n # Check whether there are any blocks left using the if conditional statement.\r\n if (gvn_n_val % sqrt_val != 0):\r\n # If it is true, then add 2 to the above-obtained perimeter of the rectangular\r\n # grid and store it in the same variable.\r\n rslt_perimetr += 2\r\n # Return the minimum perimeter of the grid made by the given n blocks.\r\n return rslt_perimetr\r\n\r\n\r\n# Give the n value as user input using the int(input()) function input and\r\n# store it in a variable.\r\ngvn_n_val = int(input(\"Enter some random number = \"))\r\n# Pass the given n value as an argument to the Minimum_perimtr()\u00a0function, convert\r\n# it into an integer using the int() function and store it in another variable.\r\nfnl_rslt = int(Minimum_perimtr(gvn_n_val))\r\n# Print the above result which is the minimum perimeter of the grid made by the\r\n# given n blocks.\r\nprint(\r\n \"The minimum perimeter of the grid made by the given n blocks{\", gvn_n_val, \"} = \", fnl_rslt)\r\n<\/pre>\n
Enter some random number = 9\r\nThe minimum perimeter of the grid made by the given n blocks{ 9 } = 12\r\n\r\n<\/pre>\n
\n