# Thread: Check if number is divisible

1. ## Check if number is divisible

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 Code:
``` <?php \$num = 8; while (is_int(\$num / 2)) { \$num / 2; } echo(\$num); ?> ```

2. I think it is:

PHP Code:
``` if(\$number % 2 == 0) { //its divisible by 2 }  ```
Silly

3. 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 Code:
``` <?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?

4. % 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.

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

6. 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

7. Wouldn't it stop when \$number == 1?

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

Silly

9. I see. He wasn't updating his \$number var.

10. Who needs a loop ?

type this bit of code

PHP 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

11. ok that function works but how would I simplify 3 numbers by a common denominator? I was thinking something like this
PHP Code:
``` \$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

12. Not quiite sure what you are after.

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

13. 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?

14. I think you are looking for the Greatest common denominator?

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

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

15. Okay, tested now.
PHP Code:
``` 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 "\n8,12 ",gcd(8,12); echo "\n84,32 ",gcd(84,32); echo "\n84,32 ",simplify(84,32);  ```
Produces:
Code:
```12,8 4
8,12 4
84,32 4
84,32 21,8```
I think that is what you are looking for.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•