467,868 Members | 1,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,868 developers. It's quick & easy.

Factoring Intergers script does not give correct results

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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. function findfacts($x) {
  5.  
  6. $highest = $x;
  7. $counter = 1;
  8. $factarray;
  9. $factarraycounter = 0;
  10.  
  11. while ($counter < ($highest / 2)) {
  12.  
  13.     if ($x % $counter = 0){
  14.  
  15.     $factarray[$factarraycounter] = $counter;
  16.     $factarraycounter++;
  17.     }
  18.  
  19.  
  20. $counter++;
  21. }
  22.  
  23. var_dump($factarray);
  24.   return $factarray;
  25. }
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. $input = 1260;
  37.  $length = strlen($input);
  38.  
  39.  
  40. $factarray = findfacts($input);
  41.  
  42.  
  43.  
  44. var_dump($factarray);
  45. ?>
  46.  
  47. <html><body></body></html>
  48.  
  49.  
  50.  
why does it totally fail?
Sep 8 '07 #1
  • viewed: 1305
Share:
11 Replies
pbmods
Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. if ($x % $counter = 0){
  2.  
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.
Sep 8 '07 #2
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. :)
Sep 9 '07 #3
kovik
Expert 1GB
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. $factsArrayCounter), better indentation, not so much white space from line to line, etc.

Also, why do you var_dump inside and outside of the function...?
Sep 9 '07 #4
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.
Expand|Select|Wrap|Line Numbers
  1.    1.
  2.       <?php
  3.    2.
  4.  
  5.    3.
  6.  
  7.    4.
  8.       function findfacts($x) {
  9.    5.
  10.  
  11.    6.
  12.  
  13.    7.
  14.       $counter = 1;
  15.    8.
  16.       $factarray;
  17.    9.
  18.       $factarraycounter = 0;
  19.   10.
  20.  
  21.   11.
  22.       while ($counter < $x) {
  23.   12.
  24.  
  25.   13.
  26.           if ($x % $counter = 0){
  27.   14.
  28.  
  29.   15.
  30.           $factarray[$factarraycounter] = $counter;
  31.   16.
  32.           $factarraycounter++;
  33.   17.
  34.           }
  35.   18.
  36.  
  37.   19.
  38.  
  39.   20.
  40.       $counter++;
  41.   21.
  42.       }
  43.   22.
  44.  
  45.   23.
  46.       var_dump($factarray);
  47.   24.
  48.         return $factarray;
  49.   25.
  50.       }
  51.   26.
  52.  
  53.   27.
  54.  
  55.   28.
  56.  
  57.   29.
  58.  
  59.   30.
  60.  
  61.   31.
  62.  
  63.   32.
  64.  
  65.   33.
  66.  
  67.   34.
  68.  
  69.   35.
  70.  
  71.   36.
  72.       $input = 1260;
  73.   37.
  74.  
  75.   38.
  76.  
  77.   39.
  78.  
  79.   40.
  80.       $factarray = findfacts($input);
  81.   41.
  82.  
  83.   42.
  84.  
  85.   43.
  86.  
  87.   44.
  88.       var_dump($factarray);
  89.   45.
  90.       ?>
  91.   46.
  92.  
  93.   47.
  94.       <html><body></body></html>
  95.  
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.
Sep 12 '07 #5
pbmods
Expert 4TB
Heya, Taorluath.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]
Sep 12 '07 #6
Sorry about that, I just tried to copy what I had posted. It didn't really work.
Sep 13 '07 #7
pbmods
Expert 4TB
Heay, Taorluath.

This line is still your problem:
Expand|Select|Wrap|Line Numbers
  1.  if ($x % $counter = 0){
  2.  
Sep 13 '07 #8
Heay, Taorluath.

This line is still your problem:
Expand|Select|Wrap|Line Numbers
  1.  if ($x % $counter = 0){
  2.  
What's wrong with it?
Sep 13 '07 #9
code green
Expert 1GB
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
Sep 14 '07 #10
Dang it!!!
How did I not see that?? Thanks a lot.
Sep 14 '07 #11
code green
Expert 1GB
This is one of those that you need to sleep on then come back to.
Sep 14 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Robert | last post: by
3 posts views Thread by melih.onvural | last post: by
2 posts views Thread by Bobby | last post: by
118 posts views Thread by Chuck Cheeze | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.