Thread: Number Factorization

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    33

    Number Factorization

    Im trying to get a program that reads in a positive integer and prints out all the factors of that integer. I dont know how to word the code to do the division and only print out the factors. Any help would be greatly appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I dont know how to word the code to do the division and only print out the factors.
    Start with: how do you know that an integer is a factor of the given integer?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    by seeing if the division gives a whole number

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if I gave you (or your program) 148, what should you (or your program) print? All the possible factors, all the factors paired off, all the prime factors, something else? Once you know what the answers are, you'll know how to do it.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Take a look at the modulus (&#37 operator. For example if you were given the input number 6, and did a loop from 1 to that value(6), and did something like this
    Code:
    Make sure that the input number is greater than 1, otherwise this is trivial. Test it to make sure.
    
    for (int i = 1; i <= value; i++)
    {
        remainder = value % i;
        if (remainder == 0)
        {
            factor1 = i;
            factor2 = value / i;
            printf("FACTOR 1 = %d\n, factor1);
            printf("FACTOR 2 = %d\n, factor2);    
        }
    }

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    well i would divide it with every number between 1 and its self.. so would I set up a loop to keep dividing and then -- for the next number down?

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    so I am starting with this and i get like 15 errors
    Code:
    #include <stdio.h>
    int main (void){
    int i,value,factor1,factor2,remainder;
    
    for (int i=1; i<= value; i++)
    
        remainder = value % i;
        if (remainder == 0)
        {
            factor1 = i;
            factor2 = value / i;
            printf("FACTOR 1 = %d\n, factor1);
            printf("FACTOR 2 = %d\n, factor2);    
        }
    }
    system("pause");
    
    return 0
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by clipsit View Post
    so I am starting with this and i get like 15 errors
    Code:
    #include <stdio.h>
    int main (void){
    int i,value,factor1,factor2,remainder;
    
    for (int i=1; i<= value; i++)
    
        remainder = value % i;
        if (remainder == 0)
        {
            factor1 = i;
            factor2 = value / i;
            printf("FACTOR 1 = %d\n, factor1);
            printf("FACTOR 2 = %d\n, factor2);    
        }
    }
    system("pause");
    
    return 0
    }
    Well, yeah, I suppose: the body of your for-loop is not in {curly braces}, you don't have any close quotes in your printf statements, you don't have a semicolon after your return statement, you can't declare new variables inside the parentheses of a for() statement, and even if you could you've already got an integer named i. And, system is in the stdlib library, so you would need to include that header file too. I think that's it.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You only need to test up to half the value since nothing greater can possibly divide it evenly.
    And you only really need odd numbers (and test 2 separately).

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    Ok all that is running smoothly now added in some print and scan statements at beginning. How do I get it to not repeat any numbers?
    Code:
    #include <stdio.h>
    int main (void){
    int i,value,factor1,factor2,remainder;
    printf("Please enter the inter you want to factor.\n");
    scanf("%d",&value);
    
    for (i=1; i<=value; i++)
    {
        remainder = value % i;
        if (remainder == 0)
        {
            factor1 = i;
            factor2 = value / i;
            printf("FACTOR 1 = %d\n", factor1);
            printf("FACTOR 2 = %d\n", factor2);    
        }
    }
    system("pause");
    
    return 0;
    
    }
    for example when run with 10
    i get
    1
    10
    2
    5
    5
    2
    10
    1

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You only need to test up to half the value since nothing greater can possibly divide it evenly.
    Actually, to find all factors or pairs of factors one only needs to test to the square root of the value.

    And you only really need odd numbers (and test 2 separately).
    Not true, since multiples of two can also be valid factors (e.g., 12 = 2 * 6, but also 3 * 4).

    Of course, this is assuming that clipsit wants to find all factors or pairs of factors instead of the prime factors. clipsit, you need to be specific about this requirement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    only guideline is "Create a list of each of the positive factors of the given integer"

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah.

    How do I get it to not repeat any numbers?
    As I noted, you need to terminate the loop once you reach the square root of the value. This happens when factor1 >= factor2, so you just need to keep looping while factor1 < factor2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    sorry Im still really new how do i terminate the loop. I set up the if statement but dont know where to go from there.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    how do i terminate the loop
    Either using the loop condition or with a break.

    I set up the if statement but dont know where to go from there.
    What is your current code? Good to see that you are using code tags, but also try and indent your code consistently.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM