Go Min Perimeter Rectangle
func minPerimeterRectangle(n int) int {
minPerimeter := math.MaxInt
for i := 1; i*i < n; i++ {
if n%i == 0 {
if p := 2 * (i + n/i); p < minPerimeter {
minPerimeter = p
}
}
}
return minPerimeter
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Haskell Min Perimeter Rectangle
minPerimeterRectangle :: Int -> Int
minPerimeterRectangle n = go 1 maxBound
where
go i best
| i * i >= n = best
| otherwise = go (i + 1) newBest
where
newBest = if n `mod` i == 0 then min best (2 * (i + n `div` i)) else best
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Java Min Perimeter Rectangle
public class Solution {
public static int minPerimeterRectangle(int n) {
long i = 1;
long min = Long.MAX_VALUE;
while (i * i < n) {
if (n % i == 0) {
min = Math.min(min, 2 * (i + n / i));
}
i++;
}
return (int) min;
}
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Lisp Min Perimeter Rectangle
(defun min-perimeter-rectangle (n)
(let ((i 1)
(min most-positive-fixnum))
(loop while (< (* i i) n)
do (progn
(when (zerop (mod n i))
(setf min (min min (* 2 (+ i (/ n i))))))
(incf i)))
min))
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
PHP Min Perimeter Rectangle
function minPerimeterRectangle(int $n): int
{
$i = 1;
$min = PHP_INT_MAX;
while ($i * $i < $n) {
if ($n % $i === 0) {
$min = min($min, (2 * ($i + $n / $i)));
}
$i++;
}
return $min;
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Python Min Perimeter Rectangle
def min_perimeter_rectangle(n: int) -> int:
i = 1
min_perimeter = float("inf")
while i * i < n:
if n % i == 0:
min_perimeter = min(min_perimeter, 2 * (i + n // i))
i += 1
return int(min_perimeter)
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Rust Min Perimeter Rectangle
fn min_perimeter_rectangle(n: i64) -> i64 {
let mut i = 1i64;
let mut min = i64::MAX;
while i * i < n {
if n % i == 0 {
min = min.min(2 * (i + n / i));
}
i += 1;
}
min
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
TypeScript Min Perimeter Rectangle
function minPerimeterRectangle(n: number): number {
let i = 1;
let min = Infinity;
while (i * i < n) {
if (n % i === 0) {
min = Math.min(min, 2 * (i + n / i));
}
i++;
}
return min;
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Bash Missing Integer
missing_integer() {
local -n _a="$1"
local -a _sorted=($(printf '%s\n' "${_a[@]}" | sort -nu))
local _min=1 _v
for _v in "${_sorted[@]}"; do
if (( _v > 0 )); then
if (( _min != _v )); then
break
fi
((_min++))
fi
done
echo "$_min"
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
C++ Missing Integer
#include <algorithm>
#include <vector>
int missingInteger(std::vector<int> a)
{
std::sort(a.begin(), a.end());
a.erase(std::unique(a.begin(), a.end()), a.end());
int min = 1;
for (int v : a) {
if (v > 0) {
if (min != v) {
break;
}
++min;
}
}
return min;
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.