检查一个数字是否能被 6 整除 PHP

发布于 2024-08-18 11:35:02 字数 64 浏览 2 评论 0原文

我想检查一个数字是否可以被 6 整除,如果不能,我需要增加它直到它可以被 6 整除。

我该怎么办?

I want to check if a number is divisible by 6 and if not I need to increase it until it becomes divisible.

how can I do that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

多情出卖 2024-08-25 11:35:02
if ($number % 6 != 0) {
  $number += 6 - ($number % 6);
}

modulus 运算符给出除法的余数,所以 $number % 6 是除以 6 时剩下的金额。这比循环并不断重新检查要快。

如果减少是可以接受的,那么这甚至更快:

$number -= $number % 6;
if ($number % 6 != 0) {
  $number += 6 - ($number % 6);
}

The modulus operator gives the remainder of the division, so $number % 6 is the amount left over when dividing by 6. This will be faster than doing a loop and continually rechecking.

If decreasing is acceptable then this is even faster:

$number -= $number % 6;
污味仙女 2024-08-25 11:35:02
if ($variable % 6 == 0) {
    echo 'This number is divisible by 6.';
}:

能被 6 整除:

$variable += (6 - ($variable % 6)) % 6; // faster than while for large divisors
if ($variable % 6 == 0) {
    echo 'This number is divisible by 6.';
}:

Make divisible by 6:

$variable += (6 - ($variable % 6)) % 6; // faster than while for large divisors
方圜几里 2024-08-25 11:35:02
$num += (6-$num%6)%6;

不需要 while 循环!模 (%) 返回除法的余数。 IE 20%6 = 2。6-2 = 4。20+4 = 24。24 可以被 6 整除。

$num += (6-$num%6)%6;

no need for a while loop! Modulo (%) returns the remainder of a division. IE 20%6 = 2. 6-2 = 4. 20+4 = 24. 24 is divisible by 6.

冷清清 2024-08-25 11:35:02

那么你想要下一个 6 的倍数,是吗?

您可以将您的数字除以 6,然后 ceil< /a> 它,并再次相乘:

$answer = ceil($foo / 6) * 6;

So you want the next multiple of 6, is that it?

You can divide your number by 6, then ceil it, and multiply it again:

$answer = ceil($foo / 6) * 6;
束缚m 2024-08-25 11:35:02

我看到其他一些答案调用了两次模数。

我的偏好是不要要求 php 多次执行相同的操作。因此,我缓存了其余部分。

其他开发人员可能更愿意不生成额外的全局变量,或者有其他理由使用两次模运算符。

代码:(演示

$factor = 6;
for($x = 0; $x < 10; ++$x){  // battery of 10 tests
    $number = rand( 0 , 100 );
    echo "Number: $number Becomes: ";
    if( $remainder = $number % $factor ) {  // if not zero
        $number += $factor - $remainder;  // use cached $remainder instead of calculating again
    }
    echo "$number\n";
}

可能的输出:

Number: 80 Becomes: 84
Number: 57 Becomes: 60
Number: 94 Becomes: 96
Number: 48 Becomes: 48
Number: 80 Becomes: 84
Number: 36 Becomes: 36
Number: 17 Becomes: 18
Number: 41 Becomes: 42
Number: 3 Becomes: 6
Number: 64 Becomes: 66

I see some of the other answers calling the modulo twice.

My preference is not to ask php to do the same thing more than once. For this reason, I cache the remainder.

Other devs may prefer to not generate the extra global variable or have other justifications for using modulo operator twice.

Code: (Demo)

$factor = 6;
for($x = 0; $x < 10; ++$x){  // battery of 10 tests
    $number = rand( 0 , 100 );
    echo "Number: $number Becomes: ";
    if( $remainder = $number % $factor ) {  // if not zero
        $number += $factor - $remainder;  // use cached $remainder instead of calculating again
    }
    echo "$number\n";
}

Possible Output:

Number: 80 Becomes: 84
Number: 57 Becomes: 60
Number: 94 Becomes: 96
Number: 48 Becomes: 48
Number: 80 Becomes: 84
Number: 36 Becomes: 36
Number: 17 Becomes: 18
Number: 41 Becomes: 42
Number: 3 Becomes: 6
Number: 64 Becomes: 66
避讳 2024-08-25 11:35:02

使用 Mod %(模数)运算符

if ($x % 6 == 0) return 1;


function nearest_multiple_of_6($x) {
    if ($x % 6 == 0) return $x;    

    return (($x / 6) + 1) * 6;
}

Use the Mod % (modulus) operator

if ($x % 6 == 0) return 1;


function nearest_multiple_of_6($x) {
    if ($x % 6 == 0) return $x;    

    return (($x / 6) + 1) * 6;
}
很糊涂小朋友 2024-08-25 11:35:02

只需运行一个 while 循环,该循环将继续循环(并增加数字),直到数字可以被 6 整除。

while ($number % 6 != 0) {
    $number++;
}

Simply run a while loop that will continue to loop (and increase the number) until the number is divisible by 6.

while ($number % 6 != 0) {
    $number++;
}
怪异←思 2024-08-25 11:35:02

假设 $foo 是一个整数:

$answer = (int) (floor(($foo + 5) / 6) * 6)

Assuming $foo is an integer:

$answer = (int) (floor(($foo + 5) / 6) * 6)
撩起发的微风 2024-08-25 11:35:02

对于微优化怪胎:

if ($num % 6 != 0)
    $num += 6 - $num % 6;

更多的 % 评估,但更少的分支/循环。 :-P

For micro-optimisation freaks:

if ($num % 6 != 0)
    $num += 6 - $num % 6;

More evaluations of %, but less branching/looping. :-P

尹雨沫 2024-08-25 11:35:02

为什么不使用模数运算符

试试这个:

while ($s % 6 != 0) $s++;
  

或者这就是你的意思?

<?

 $s= <some_number>;
 $k= $s % 6;

 if($k !=0)    $s=$s+6-$k;
?>

Why don't you use the Modulus Operator?

Try this:

while ($s % 6 != 0) $s++;
  

Or is this what you meant?

<?

 $s= <some_number>;
 $k= $s % 6;

 if($k !=0)    $s=$s+6-$k;
?>
蓦然回首 2024-08-25 11:35:02
result = initial number + (6 - initial number % 6)
result = initial number + (6 - initial number % 6)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文