Home c++ number of divisors of the number

number of divisors of the number

Author

Date

Category

How to find the number of dividers of the number.

For example, the number

20 - & gt; 1 2 4 5 10 20
1 - & gt; 1 = 1.
2 - & gt; 1, 2 = 2
4 - & gt; 1, 2, 4 = 3
5 - & gt; 1, 2 = 2
10 - & gt; 1, 2, 5, 10 = 4
20 - & gt; 1, 2, 4, 5, 10, 10 = 6
1 + 2 + 3 + 2 + 4 + 6 = 18

Answer 1, Authority 100%

Frankly, nothing smarter than a bust of simple divisors of the number to SQRT (N) I do not see. Well, and then – the bust of the combinations of these simple divisors into the composite dividers. It is clear that when I find a simple divider, we divide the number on it and start all over again. And it is no less clear that the amount of all combinations (== the number of dividers) is simply a product of all degrees of simple dividers, increased by 1. Well, for example, 360 = 2 ^ 3 * 3 ^ 2 * 5 ^ 1 , so the number of divisors (3 + 1) * (2 + 1) * (1 + 1) = 24 .

If the numbers are small – you can simply bust all the contractors in a row to sqrt (n) , taking into account that for each such divider, different from 1, there is an appropriate divider from the back from SQRT (N) .

Code needed? 🙂

Update: Example – Output of all divisors (including 1 and number)


Answer 2

You can find the algorithm and a little better than the search for simple divisors of the number to SQRT (N) .

Pay attention to the fact that any number can be represented as a product of simple multipliers, so if we found one simple divider, then then we do not need to look for the n dividers, but you need to look for dividers from Private N / X , where X is our simple divider.

for example. (I apologize for C #)

uint [] simpl_arr = // array filled with simple numbers
public List & lt; int & gt; GetSimpleDividors (Uint Number)
{
  List & lt; int & gt; result = new List & lt; int & gt; ();
  uint s = convert.touint32 (math.ceiling (Math.SQRT (Number)));
  for (int i = 0; simple_arr [i] & lt; s; i ++)
  {
    If (Number% Simple_Arr [i] == 0)
    {
      result.Add (simple_arr [i]);
      uint quotient = number / simple_arr [i];
      var tmp = GetSimpleDividors (quotent);
      if (tmp.Count & gt; 0)
      {
         result.AddRange (tmp);
      }
      ELSE.
      {
         result.Add (quotent);
      }
      Break;
    }
  }
  RETURN RESULT;
}

Answer 3

You can loop through the Euclidean algorithm to count to divide by 0.
Until the end of cycle check result of division if the remainder of division is equal to 0, then add 1 to the counter
.
The counter counts the number of divisors.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions