1. ## factorization issues

im writing a basic program that finds all factors of a number that the user inputs, tells the amount of factors, the sum of the factors, and the product of the factors. pretty simple... but the last part, the product of the factors, crashes with larger numbers. not ridiculously large numbers, but triple digit numbers. all other parts work fine. can anyone help?

pm me if you can help, id prefer not to post my code.
(last semester i posted my code on another site and about 15 other students in my class turned in my exact code... big mess.)

when the assignment due date passes i will post it.

2. tell at least what input number crashes the program and what type you are using for storing the product

3. stored as an integer

368 is one.

i can pm you the entire code

4. most numbers work fine. its the numbers with a lot of factors that get messed up. i dont remember what the input was, but i got a negative output once. im not sure if the code is just wrong or if there was something i left out.

edit:

"999" outputs a negative number for the product of the factors.

5. ... this is driving me insane... can anyone help?

6. How are you storing the factors? If "a lot of factors" is a problem, perhaps you don't have places to put them.

7. You didn't mention them being only the prime factors.

If they were then they would multiply back to the original number.

If however you do mean all factors then the result could get rather large.
The maximum value of a signed 32-bit int is a little over 2.1 billion. If the factors you're multiplying together would produce a result larger than that, then you'll naturally see only the last 32-bits of the result, where the result may overflow into the upper bit, causing the number to be interpreted as negative. This should not cause a crash unless you're doing something else bad as a result.

Do the multiplications using calc and see if the result is too large. If it is, you need to use a larger data type, or a 'bignum' library.

8. i can pm you the code. would you mind taking a look?

9. Originally Posted by iMalc
You didn't mention them being only the prime factors.

If they were then they would multiply back to the original number.

If however you do mean all factors then the result could get rather large.
The maximum value of a signed 32-bit int is a little over 2.1 billion. If the factors you're multiplying together would produce a result larger than that, then you'll naturally see only the last 32-bits of the result, where the result may overflow into the upper bit, causing the number to be interpreted as negative. This should not cause a crash unless you're doing something else bad as a result.

Do the multiplications using calc and see if the result is too large. If it is, you need to use a larger data type, or a 'bignum' library.
no not just the prime factors. all factors.

when i enter large numbers the product is way, way off.

10. Originally Posted by tabstop
How are you storing the factors? If "a lot of factors" is a problem, perhaps you don't have places to put them.
not sure... i am new to programming.

as i said above, my code was copied from a forum and i was accused of cheating. even after showing my professor the post he failed my assignment so i withdrew from the class. i am retaking it this semester but i only went for the first 3 weeks last semester so i still know very little about programming

11. When you use ++, you don't assign the value again (or in your case, compare it with ==).

If you multiply all the factors of 999 together, you get 996,005,996,001 (which not coincidentally is 999^4); except, of course, all math internally is done modulo 2^32 -- hence the number works out to 3,868,550,625. If 3,868,550,625 is interpreted as a signed integer, you get -426,416,671. 999^4 is a little under 2^40, so if you were using a 64-bit data type you'd be fine (for this number, not in general).

12. Originally Posted by tabstop
When you use ++, you don't assign the value again (or in your case, compare it with ==).
can you dumb this down a little for me? im not sure what you mean.

but why when i enter 998 it works? im a bit more confused now.

13. If your code looks like anything other than "num++;", it ain't right.

When you multiply all the factors together of n together, you will always get n^x for some x[0]. For instance, for 999 the power is four because there are four pairs of factors, each of which (obviously) multiplies to 999: 1 x 999, 3 x 333, 9 x 111, and 27 x 37. For 998, the pairs of factors are 1 x 998, 2 x 499, so you end up with 998^2, which is small enough to fit.

[0]x will be an integer unless n is a perfect square, in which case it will be a half-integer.

14. Originally Posted by tabstop

If you multiply all the factors of 999 together, you get 996,005,996,001 (which not coincidentally is 999^4); except, of course, all math internally is done modulo 2^32 -- hence the number works out to 3,868,550,625. If 3,868,550,625 is interpreted as a signed integer, you get -426,416,671. 999^4 is a little under 2^40, so if you were using a 64-bit data type you'd be fine (for this number, not in general).
are you saying that it is impossible to do this or that im just doing it wrong? can you pm the code back with what needs to be changed?

15. Originally Posted by tabstop
If your code looks like anything other than "num++;", it ain't right.
oh i see... so take out the "num==" out of the "num==num++"