SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Zealot Beckwith's Avatar
    Join Date
    Mar 2004
    Location
    California
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it is:

    PHP Code:

    if($number == 0)
    {

    //its divisible by 2


    Silly

  3. #3
    SitePoint Zealot Beckwith's Avatar
    Join Date
    Mar 2004
    Location
    California
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 == 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. #4
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    % 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. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $number 
    8;
    $i 0;
    while (
    $number == 0)
    {
    $number $number 2;
    $i++;
    }
    echo(
    $number ", " $i);
    ?>

  6. #6
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #7
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't it stop when $number == 1?

  8. #8
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It obviously hasnt for him as he said it timed out.

    Silly

  9. #9
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see. He wasn't updating his $number var.

  10. #10
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Who needs a loop ?

    type this bit of code

    PHP Code:
      function Simplify($Numerator$Denominator)
      {
        
    $div $Numerator $Denominator;
        
    $mod $Numerator $Denominator;
      
        if (
    == $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 (156);
      
    Simplify (155); 
    Error checking / handling is left as an exercise for the reader

  11. #11
    SitePoint Zealot Beckwith's Avatar
    Join Date
    Mar 2004
    Location
    California
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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($number12);
    $simp2 simplify($number22);
    $simp3 simplify($number32);
    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. #12
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not quiite sure what you are after.

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

  13. #13
    SitePoint Zealot Beckwith's Avatar
    Join Date
    Mar 2004
    Location
    California
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #14
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you are looking for the Greatest common denominator?

    http://www.nist.gov/dads/HTML/euclidalgo.html

    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;

    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  15. #15
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

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