How do I check whether a number is divisible by another and put it in a while statement until it can’t go anymore…

Here’s what I’ve tried so far but it doesn’t work…

```
<?php
$num = 8;
while (is_int($num / 2)) {
$num / 2;
}
echo($num);
?>
```

How do I check whether a number is divisible by another and put it in a while statement until it can’t go anymore…

Here’s what I’ve tried so far but it doesn’t work…

```
<?php
$num = 8;
while (is_int($num / 2)) {
$num / 2;
}
echo($num);
?>
```

I think it is:

```
if($number % 2 == 0)
{
//its divisible by 2
}
```

Silly

ok, that validates correctly, however I have another problem that involves putting it in a while statement. My goal is to be able to simplify two numbers by a common denominator. I also need to find out how many times it can be divided, however putting it in a while statement doesn’t seem to work, the script just times out.

```
<?php
$number = 8;
while ($number % 2 == 0)
{
$number / 2;
}
echo($number);
?>
```

However, I haven’t tried counting how many times the while statement executed correctly (to decide a least common multiple), and it also doesn’t display what the final number is. Oh and by the way, I don’t know why you used a % sign instead of my operation…what’s the difference and what does % do?

% is modulous, so 20%10 reads “20 mod 10”, and it equals 0 (20 is divisable by ten). A non-divisable number will give you a value other than zero.

```
<?php
$number = 8;
$i = 0;
while ($number % 2 == 0)
{
$number = $number / 2;
$i++;
}
echo($number . ", " . $i);
?>
```

Your script will time out, there is no ending condition. It will keep going until your script times out or runs out of memory. You need a condition to stop it.

Silly

Wouldn’t it stop when $number == 1?

It obviously hasnt for him as he said it timed out.

Silly

I see. He wasn’t updating his $number var.

Who needs a loop ?

type this bit of code

```
function Simplify($Numerator, $Denominator)
{
$div = $Numerator / $Denominator;
$mod = $Numerator % $Denominator;
if (0 == $mod)
{
echo $Numerator . ' is exactly divisible by ' . $Denominator . ' - ' . $div . ' times <br />';
}
else
{
echo $Numerator . ' is not exactly divisible by ' . $Denominator . '<br />';
}
}
// test the function
Simplify (15, 6);
Simplify (15, 5);
```

Error checking / handling is left as an exercise for the reader

ok that function works but how would I simplify 3 numbers by a common denominator? I was thinking something like this

```
$number1 = 32;
$number2 = 56;
$number3 = 84;
$simp1 = simplify($number1, 2);
$simp2 = simplify($number2, 2);
$simp3 = simplify($number3, 2);
if ($simp1 < $simp2 and $simp1 < $simp3) {
$i = 0;
while ($i < $numberoftimesdivided) {
$number1 = $number1 / 2;
$number2 = $number2 / 2;
$number3 = $number3 / 2;
$i++;
}
}
```

and then for numbers 2 and 3. However, I don’t think the function you wrote is setup to do something like that, is it? I don’t think my variable $numberoftimesdivided can be harvested from it…

Thanks

Not quiite sure what you are after.

Given your 3 numbers, 32, 56 and 84, what do you want the results to be?

well, since 32 can simplify to 2, simplifying 4 times. 56 simplifies to 7, 3 times. 84 simplifies to 21, 2 times. So, since 84 simplifies the least, divide each number by 2, twice, leaving all the numbers simplified under a common denominator Any ideas?

I think you are looking for the Greatest common denominator?

I believe this would be a PHP implementation of Euclid’s algorithm (not tested)

```
function gcd($a, $b) {
return ($b) ? gcd($b, $a % $b) : $a;
}
```

Okay, tested now.

```
function gcd($a, $b) {
return ($b) ? gcd($b, $a % $b) : $a;
}
function simplify($a,$b) {
$gcd = gcd($a,$b);
echo $a/$gcd, ',',$b/$gcd;
}
echo '12,8 ',gcd(12,8);
echo "\
8,12 ",gcd(8,12);
echo "\
84,32 ",gcd(84,32);
echo "\
84,32 ",simplify(84,32);
```

Produces:

```
12,8 4
8,12 4
84,32 4
84,32 21,8
```

I think that is what you are looking for.