Factors code

Hi, i've written some code that returns the factors of any number inputted by the user. There are a few problems i'm having though. The main one is that the code works for any number thats given up to a certain point. For example, if i input 100,000 it returns all the correct factors up to 50,000 (1, 2, 4..., 20000, 25000, 50000) etc and then randomly gives some other numbers like 50128 and 85682.

Another thing i've also noticed that i'm not worried about at the moment is that with large numbers like this the code is not very efficient but i'll come back to that at another time. Could this be part of the reason though?

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void factors(double &num)
{
    cout << "The factors of " << (int)num << " are: ";
    
    int i, mult;
    
    for(i=1; i<=num; i++)
      {
        for(mult=1;mult<=num;mult++)
          {
           if((i*mult)==num)cout << i << ", ";
          }
      }
    
    num = 0;
    cout << "\n\n";
    return;
}


Oh and before anyone says anything you have to see the rest of the code to understand why i've used an int cast on the variable num.
You are exceeding the size of the int datatype. If you input 100,000, (i*mult) = 1*1010, which is greater than the maximum size of an int on 32-bit systems. You can double the range by using unsigned int, but you can also change line 9 so that you do not end up checking impossible factors.
for(mult=1;mult<=num/i;mult++)
The long timing is caused by the two loops, using something like this should give the right result in much less time:
1
2
for(i=1; i<=num; i++)
     if (!(num%i)) cout << i << ", ";

and I'm sure that there are some more efficient algorithms to do that
You're also checking if an integer is equal to a double, meaning they both get cast to doubles. Better to compare them both as ints.

1
2
3
4
5
6
7
8
9
10
11
void factors(double &num)
{
	int num2 = (int) num;
    cout << "The factors of " << num2 << " are: ";
    for(int i=1; i<=num2; i++) {
		  if (!(num2 % i))
			cout << i << ", ";
      }
    num = 0;
    cout << "\n\n";
}
ahh thanks to everyone for your replies i've sorted it all out now. One quick question though about what JDD said.

which is greater than the maximum size of an int on 32-bit systems.


I'm running a 64-bit system so shouldn't this technically work for me?
Someone else may be able to explain this better, but my understanding is that the sizes of datatypes are implementation defined and have no standard size (the only exception is char, which is always the same). Even among similar systems, say all 64 bit, there is no guarantee that datatypes will have the same size. If you are curious, you can use sizeof to find the number of bytes that any given datatype occupies.
int is guaranteed to be machine word size, which means on a 64-bit proc compiled to 64-bit code int should be 64 bits. The question is: is the compiler generating 64-bit code or 32-bit code?

OP should do jdd's suggestion of printing sizeof int.
i went away and compiled this:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main()
{
    int a;
    cout << sizeof(a);
    
    cin.get();
    return 0;
}


the output was 4. Also tried giving it a value (didn't know if it would make a difference) and it didnt. So what does his mean then, it occupied 4 bytes but is this 32-bit or 64?
That's 32-bit.
Believe it or not I just read about this earlier today. Trying to determine "n-bit" types is actually kind of funky because there's no guarantee that things align to 8-bits.

- sizeof(char) is always 1. No exceptions.
- sizeof() returns the number of "byte"s.
- 1 "byte" is not necessarily 8 bits, but can be a larger number of bits
- 1 "byte" is a minimum of 8 bits (can't be smaller, can be larger)
- CHAR_BIT in <climits> gives you the bitwidth of a "byte"

As for other types, I'm not sure what they're really defined as, but they probably must be all at least 1 byte in size (bools included -- although multiple bools might get "merged" into a bitfield by the compiler).

min/max ranges for types such as 'int' and 'long' can also be determined with <climits>, or possibly with <limits>... although <limits> can't be used in preprocessor #ifdefs =(

Source (and very interesting read):
http://www.parashift.com/c++-faq-lite/intrinsic-types.html#faq-26.1

Anyway this all seems to reaffim my motto. typedef typedef typedef.
Thanks for your reply guys, i'll have a read of that article later!
Topic archived. No new replies allowed.