Thread: Finding divisors

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    45

    Angry Finding divisors

    Ok the divisors of 10 are 1 , 2 , 5 , 10 . Ok I'm making a program, but I can't think of a way to find the divisor of a number. Example if someone types 6 how do I find it's divisor? I just need help on te math portion.

    Note: I'm not asking people to do this code for me I just need some help on finding the divisor of a number.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Do you know how to build a factor tree?

    You can do this recursively. If it is even, divide in half, and factor again. If the number is odd, check for prime, otherwise factor the composite.

    1 and the integer are given factors of any integer.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by citizen View Post
    Do you know how to build a factor tree?

    You can do this recursively. If it is even, divide in half, and factor again. If the number is odd, check for prime, otherwise factor the composite.

    1 and the integer are given factors of any integer.
    so num /= 2?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can do a very basic type of proceedure for brute forcing it.

    Example:
    Code:
    int nfactors(int number, std::vector<int> &factors)
    {
      int n = 0, i = 1, limit = number;
    
      factors.clear();
    
      for(;i < limit; ++i)
         if(!(number&#37;i))
         {
           limit = number/i;
           factors.push_back(limit);
           ++n;
    
           if(limit != i)
           {
              factors.push_back(i);
              ++n;
           }
         }
    
      // One could sort the results at this point, but bleh... you didn't specify that.
    
      return n;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by master5001
    You can do a very basic type of proceedure for brute forcing it.
    n seems rather redundant since there is factors.size().
    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

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    True.... I have no explanation for that other than I have been getting less and less sleep each passing day. Though, that is hardly a valid excuse for a programmer.

    Are you trying to reduce fractions, xbusterx? You could always use the Euclidean Algorithm. That would be a much more apt method for finding a greatest common factor.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by citizen View Post
    Do you know how to build a factor tree?

    You can do this recursively. If it is even, divide in half, and factor again. If the number is odd, check for prime, otherwise factor the composite.

    1 and the integer are given factors of any integer.
    I know that but even numbers don't just divide by 2 , they divide by 3 , 5 , 5 etc as well. I know I have to use num % n1 = 0 . But ya I'm lost on the much part plz help.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by xbusterx View Post
    I know that but even numbers don't just divide by 2 , they divide by 3 , 5 , 5 etc as well. I know I have to use num % n1 = 0 . But ya I'm lost on the much part plz help.
    So check all the numbers. num%2, num%3, num%4, ....

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    So check all the numbers. num%2, num%3, num%4, ....
    ok what if I say this:

    Code:
    int n;
    cout << enter number;
    cin >> n; 
    
    for (int i = 1 ; i < n ; i++ )
    
    if ( n % i = 0 )
    how do I extract the divisor?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by xbusterx View Post
    ok what if I say this:

    Code:
    int n;
    cout << enter number;
    cin >> n; 
    
    for (int i = 1 ; i < n ; i++ )
    
    if ( n % i = 0 )
    how do I extract the divisor?
    Divide?

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    Divide?
    No that's not what I mean. IF ( n % i = 0 ) that means i is a divisor, but how do I extract the divisors so I can add them? sorry I'm new.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by xbusterx View Post
    No that's not what I mean. IF ( n % i = 0 ) that means i is a divisor, but how do I extract the divisors so I can add them? sorry I'm new.
    You don't have to extract any divisors -- you just said that i was a divisor. So that's the number you have to add.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    You don't have to extract any divisors -- you just said that i was a divisor. So that's the number you have to add.
    exactly you're right , BUT how do I add them? that's why I need to exact the divisors to add them.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by xbusterx View Post
    exactly you're right , BUT how do I add them? that's why I need to exact the divisors to add them.
    I usually add numbers with "+".

    I mean, you know what a running total is, right?

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    I usually add numbers with "+".

    I mean, you know what a running total is, right?
    um like sum += num ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. display of divisors
    By Lissa in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2008, 11:20 AM
  3. Finding primes
    By starripper in forum C++ Programming
    Replies: 19
    Last Post: 01-14-2006, 04:17 PM
  4. Help with Computing Divisors
    By MrPink in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2005, 07:35 PM
  5. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM