一个数字的最大两个倍数使用PHP


Greatest two multiple of a number using PHP

我想创建一个表,其中的列和行是根据用户输入的数字动态计算的。为此,我想计算最接近的两个数字作为一个数字的列和行。

例如,如果用户输入10,则脚本计算行为3或4,列为3或4,例如sum将变为10;

3*3+1 = 10

如果有人输入4,那么应该生成2列和2行。返回2和2如果是10,则生成3列和4行。返回3和4

这里需要的是找到输入的平方根并获得底数值。例如:当输入= 10时,sqrt(10) = 3.16227766017。

现在,得到地板值,即3。尺寸是3 × 3

下一步,检查是否需要添加一些内容。公式可以是:Nearest = Input - (Dimension * Dimension)

所以,最后的计算是维度*维度+最近邻(如果不是0)

这将解决你的目的:

$dim = floor(sqrt($input));
$nearest = $input - ($dim * $dim);
if ($nearest) {
    printf("Nearest dimensions of %d = %d x %d + %d", $input, $dim, $dim,     $nearest);
} else {
    printf("Nearest dimensions of %d = %d x %d", $input, $dim, $dim);
}

$input = 4时,输出:Nearest dimensions of 4 = 2 x 2

$input = 10时,输出:Nearest dimensions of 10 = 3 x 3 + 1