Number Of Divisors (NOD)

Number of Divisors(NOD)

Given N you have to find the number of divisors of N.

Solution:
Step 1: Generating Prime using Sieve of Eratosthenes
Step 2: Count Divisors using prime factorization

CODE:

  1. long long NOD_using_factorization( long long n ) /// O(lof2(N))
  2. {
  3. long long sqrtn = sqrt ( n );
  4. long long tmp = 1,divisors = 1;
  5. for ( int i = 0; i < prime.size() && prime[i] <= sqrtn; i++ )
  6. {
  7. ///if ( sieve[n] == 0 ) break; ///*Checks if n is prime or not*/ Please comment This line if n > 1e7
  8. if ( n % prime[i] == 0 )
  9. {
  10. tmp = 1;
  11. while ( n % prime[i] == 0 )
  12. {
  13. n /= prime[i];
  14. tmp++;
  15. }
  16. divisors *=tmp;
  17. sqrtn = sqrt ( n );
  18. }
  19. }
  20. if ( n != 1 )
  21. {
  22. divisors*=2;
  23. }
  24. return divisors;
  25. }
  26.  

Comments

Popular posts from this blog

URI 2994 Solution

URI 2996 Solution