factorization issues

This is a discussion on factorization issues within the C Programming forums, part of the General Programming Boards category; im writing a basic program that finds all factors of a number that the user inputs, tells the amount of ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    66

    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.

    thanks in advance.
    Last edited by ominub; 02-01-2009 at 12:33 AM. Reason: .

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    tell at least what input number crashes the program and what type you are using for storing the product
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    stored as an integer

    368 is one.

    i can pm you the entire code

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    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.
    Last edited by ominub; 02-01-2009 at 12:49 AM. Reason: dee-dee-dee

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    ... this is driving me insane... can anyone help?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How are you storing the factors? If "a lot of factors" is a problem, perhaps you don't have places to put them.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    i can pm you the code. would you mind taking a look?

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Quote Originally Posted by iMalc View Post
    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. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Quote Originally Posted by tabstop View Post
    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. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Quote Originally Posted by tabstop View Post
    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. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Quote Originally Posted by tabstop View Post

    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. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Quote Originally Posted by tabstop View Post
    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++"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Better spacing issues
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2008, 03:46 PM
  2. Bitmap scroll issues
    By Gerread in forum Windows Programming
    Replies: 4
    Last Post: 05-14-2007, 05:18 AM
  3. Major game issues
    By VOX in forum Game Programming
    Replies: 0
    Last Post: 01-18-2005, 07:40 AM
  4. Multi-platform support issues.
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-27-2004, 06:41 PM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 08:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21