473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1670
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.