# Finding divisors

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-01-2008
xbusterx
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.
• 10-02-2008
whiteflags
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.
• 10-02-2008
xbusterx
Quote:

Originally Posted by citizen
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?
• 10-02-2008
master5001
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; }```
• 10-02-2008
laserlight
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().
• 10-02-2008
master5001
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.
• 10-04-2008
xbusterx
Quote:

Originally Posted by citizen
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.
• 10-04-2008
tabstop
Quote:

Originally Posted by xbusterx
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, ....
• 10-04-2008
xbusterx
Quote:

Originally Posted by tabstop
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-04-2008
tabstop
Quote:

Originally Posted by xbusterx
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?
• 10-04-2008
xbusterx
Quote:

Originally Posted by tabstop
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.
• 10-04-2008
tabstop
Quote:

Originally Posted by xbusterx
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.
• 10-04-2008
xbusterx
Quote:

Originally Posted by tabstop
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.
• 10-04-2008
tabstop
Quote:

Originally Posted by xbusterx
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?
• 10-04-2008
xbusterx
Quote:

Originally Posted by tabstop
I usually add numbers with "+".

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

um like sum += num ?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last