Factoring Intergers script does not give correct results

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Taorluath
    New Member
    • Sep 2007
    • 9

    Factoring Intergers script does not give correct results

    ok, so this little script is supposed to list the factors of intergers.

    [CODE=php]<?php


    function findfacts($x) {

    $highest = $x;
    $counter = 1;
    $factarray;
    $factarraycount er = 0;

    while ($counter < ($highest / 2)) {

    if ($x % $counter = 0){

    $factarray[$factarraycount er] = $counter;
    $factarraycount er++;
    }


    $counter++;
    }

    var_dump($facta rray);
    return $factarray;
    }










    $input = 1260;
    $length = strlen($input);


    $factarray = findfacts($inpu t);



    var_dump($facta rray);
    ?>

    <html><body></body></html>


    [/CODE]

    why does it totally fail?
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Taorluath. Welcome to TSDN!

    Changed thread title to better describe the problem (did you know that threads whose titles contain three words or less actually get FEWER responses?).

    If I had to guess, I'd say that this line is the problem:
    [code=php]
    if ($x % $counter = 0){
    [/code]

    What do you want your code to do? Give an example.
    What is your code doing that you don't want it to do? Give an example.
    What is your code *not* doing that it is supposed to? Give an example.

    Comment

    • JeremyMiller
      New Member
      • Sep 2007
      • 69

      #3
      You really haven't fully stated the problem, either, if I'm guessing your intention correctly. You want the unique prime factorization of this, right? Is this for a class? If so, it'd help to know at what level you happen to be as there are a few ways of solving this problem.

      BTW: "Totally fail" is not a good description of what goes wrong. :)

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        I agree with pbmods. That looks like the problem.

        If this is for a class (and even if it isn't) you should really work on the layout of your code for readability. Camel casing (i.e. $factsArrayCoun ter), better indentation, not so much white space from line to line, etc.

        Also, why do you var_dump inside and outside of the function...?

        Comment

        • Taorluath
          New Member
          • Sep 2007
          • 9

          #5
          Sorry, Sorry!!
          I had to leave on a trip right after I posted this, so I didn't really explain it.
          basically, I get a number, (1260) and I try to find all the factors.
          lines 11-21 try to do this.

          first, in line 11, we start the loop to go through every number(counter) under x.
          in line 13, we see if counter is a factor of x, (if the remainder is 0, then it should be a factor)
          if it is, we add it to the factarray array.

          the var dumps are there for me to kind of see where the error is.
          [code=php]
          1.
          <?php
          2.

          3.

          4.
          function findfacts($x) {
          5.

          6.

          7.
          $counter = 1;
          8.
          $factarray;
          9.
          $factarraycount er = 0;
          10.

          11.
          while ($counter < $x) {
          12.

          13.
          if ($x % $counter = 0){
          14.

          15.
          $factarray[$factarraycount er] = $counter;
          16.
          $factarraycount er++;
          17.
          }
          18.

          19.

          20.
          $counter++;
          21.
          }
          22.

          23.
          var_dump($facta rray);
          24.
          return $factarray;
          25.
          }
          26.

          27.

          28.

          29.

          30.

          31.

          32.

          33.

          34.

          35.

          36.
          $input = 1260;
          37.

          38.

          39.

          40.
          $factarray = findfacts($inpu t);
          41.

          42.

          43.

          44.
          var_dump($facta rray);
          45.
          ?>
          46.

          47.
          <html><body></body></html>
          [/code]

          any suggestions?

          p.s. I don't really want prime factors, just all the factors: 1,2,3,6,9,18 for 18. example.
          It's for a really basic programming club I belong to. Stress on basic.
          Last edited by Taorluath; Sep 12 '07, 02:00 AM. Reason: i had to add stuff

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Taorluath.

            Please use CODE tags when posting source code:

            &#91;CODE=ph p]
            PHP code goes here.
            &#91;/CODE]

            Comment

            • Taorluath
              New Member
              • Sep 2007
              • 9

              #7
              Sorry about that, I just tried to copy what I had posted. It didn't really work.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heay, Taorluath.

                This line is still your problem:
                [code=php]
                if ($x % $counter = 0){
                [/code]

                Comment

                • Taorluath
                  New Member
                  • Sep 2007
                  • 9

                  #9
                  Originally posted by pbmods
                  Heay, Taorluath.

                  This line is still your problem:
                  [code=php]
                  if ($x % $counter = 0){
                  [/code]
                  What's wrong with it?

                  Comment

                  • code green
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1726

                    #10
                    This line is still your problem:
                    [PHP]if ($x % $counter = 0){ [/PHP]
                    What's wrong with it?
                    This is a basic programming mistake. If you look at it hard enough you should be able to spot it.
                    No?
                    You are not testing if the remainder is zero.
                    You are SETTING counter to zero

                    Comment

                    • Taorluath
                      New Member
                      • Sep 2007
                      • 9

                      #11
                      Dang it!!!
                      How did I not see that?? Thanks a lot.

                      Comment

                      • code green
                        Recognized Expert Top Contributor
                        • Mar 2007
                        • 1726

                        #12
                        This is one of those that you need to sleep on then come back to.

                        Comment

                        Working...