1. ## 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. 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. 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?

4. 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. 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().

6. 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. 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.

8. 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, ....

9. 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. 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?

11. 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.

12. 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.

13. 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.

14. 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?

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

I mean, you know what a running total is, right?
um like sum += num ?
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}