{"id":432,"date":"2020-04-01T06:22:00","date_gmt":"2020-04-01T06:22:00","guid":{"rendered":"https:\/\/danwritescode.com\/?p=432"},"modified":"2020-06-26T06:46:45","modified_gmt":"2020-06-26T06:46:45","slug":"min-perimeter-rectangle-codility-100-correct-javascript-solution","status":"publish","type":"post","link":"https:\/\/danwritescode.com\/min-perimeter-rectangle-codility-100-correct-javascript-solution\/","title":{"rendered":"Min Perimeter Rectangle – Codility 100% Correct Javascript Solution"},"content":{"rendered":"\n

The Min Perimeter Rectangle challenge brings us back to a lot of math. I had a gut feeling on this one and I only checked with a few examples but the minimum perimeter of a rectangle for a given area happens when the rectangle is in fact a square.<\/p>\n\n\n\n

For those of you who want a to be 100% sure of this, here is a proper demonstration<\/a>.<\/p>\n\n\n\n

So all we need to do in this case is to find the closest integer number to the square root which is a dividing factor for the given area.<\/p>\n\n\n\n

Solution in Javascript with O(sqrt(N)) complexity<\/h2>\n\n\n\n
function solution(N) {\n  \/\/ write your code in JavaScript (Node.js 8.9.4)\n  for (let i = Math.floor(Math.sqrt(N)); i > 0; i--) {\n    if (N % i === 0) {\n      return (i + N \/ i) * 2;\n    }\n  }\n}<\/code><\/pre>\n\n\n\n

And that’s it.
Here<\/a> you can see how it works and the full description of the problem.<\/p>\n","protected":false},"excerpt":{"rendered":"

The Min Perimeter Rectangle challenge brings us back to a lot of math. I had a gut feeling on this one and I only checked with a few examples but the minimum perimeter of a rectangle for a given area happens when the rectangle is in fact a square. For those of you who want a to be 100% sure of this, here is a proper demonstration. So all we need to do in this case is to find the closest integer number to the square root which is a dividing factor for the given area. Solution in Javascript with <\/p>\n","protected":false},"author":1,"featured_media":433,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,12,6],"tags":[],"_links":{"self":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/432"}],"collection":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/comments?post=432"}],"version-history":[{"count":0,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/432\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media\/433"}],"wp:attachment":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media?parent=432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/categories?post=432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/tags?post=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}