Advertisement
icatalin

Numarul divizorilor unui numar | Algoritmi eficienti

Oct 14th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. //Varianta 1:
  2.  
  3. int numar_divizori(int x)
  4. {
  5.         int c=0,div;
  6.  
  7.     for (div=1; div * div < x; div++)  // div se duce pana la sqrt x
  8.         if (x % div == 0)
  9.         c+=2;
  10.  
  11.     if ( div * div == x)  // in cazul in care e patrat perfect
  12.         c++;
  13.  
  14.     return c;
  15. }
  16.  
  17. //Varianta 2:
  18.  
  19. int number_of_divisors(int x)
  20. {
  21.     int limit = x;
  22.     int numberOfDivisors = 0;
  23.  
  24.     if (x == 1) return 1;
  25.  
  26.     for (int i = 1; i < limit; ++i)
  27.         if (x % i == 0)
  28.             {
  29.             limit = x / i;
  30.             if (limit != i)
  31.                 {
  32.                     numberOfDivisors++;
  33.                 }
  34.             numberOfDivisors++;
  35.             }
  36.  
  37.  
  38.     return numberOfDivisors;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement