472,967 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,967 software developers and data experts.

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
11 1648
pbmods
5,821 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
1,044 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
5,821 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
5,821 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
1,726 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
1,726 Expert 1GB
This is one of those that you need to sleep on then come back to.
Sep 14 '07 #12

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

Similar topics

3
by: Robert | last post by:
Hello, I am trying to get output formatted from a non-php script that I post to. Example: <form method=POST action=www.myurl.com/ColdFusion.cfm> bla bla bla </form>
3
by: melih.onvural | last post by:
Group, I want to get into a remote server, tail a file, and see if the last line is an error or not. I think that I've figured out how to shell over and tail the file. I have the specific server...
2
by: Bobby | last post by:
Hello everyone I have a question. The school I am working for is in the beginning process of having a webpage that will direct students to download there homework and be able to view there info...
4
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is...
16
by: marc_r_bertrand | last post by:
To all asp/db pros: The quiz code below works. But there is a problem when too many questions are answered (radio buttons clicked). I am not an asp pro. So, is there a pro out there or an...
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
118
by: Chuck Cheeze | last post by:
This might be in the wrong group, but... Here is an example of my data: entry_id cat_id 1 20 2 25 3 30 4 25 5 35
5
by: Christopher Brewster | last post by:
I am running the same script on the same data on two different machines (the folder is synchronised with Dropbox). I get two different results. All the script does is count words in different...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.