Counting divisors of a number using JavaScript

We are required to write a JavaScript function that takes in a number and returns the count of its divisors.

Problem Statement

Input:

const num = 30;

Expected Output:

const output = 8;

Because the divisors of 30 are:

1, 2, 3, 5, 6, 10, 15, 30

Method 1: Simple Iteration Approach

The straightforward approach is to iterate through all numbers from 1 to the given number and count those that divide evenly.

const countDivisorsSimple = (num) => {
    let count = 0;
    for (let i = 1; i <= num; i++) {
        if (num % i === 0) {
            count++;
        }
    }
    return count;
};

const num = 30;
console.log("Number:", num);
console.log("Divisor count:", countDivisorsSimple(num));

// Let's also print the divisors
const findDivisors = (num) => {
    let divisors = [];
    for (let i = 1; i <= num; i++) {
        if (num % i === 0) {
            divisors.push(i);
        }
    }
    return divisors;
};

console.log("Divisors:", findDivisors(num));
Number: 30
Divisor count: 8
Divisors: [1, 2, 3, 5, 6, 10, 15, 30]

Method 2: Optimized Square Root Approach

We can optimize by iterating only up to the square root of the number, since divisors come in pairs.

const countDivisorsOptimized = (num) => {
    let count = 0;
    const sqrt = Math.sqrt(num);
    
    for (let i = 1; i <= sqrt; i++) {
        if (num % i === 0) {
            // If i divides num, then both i and num/i are divisors
            if (i === sqrt) {
                count++; // Perfect square case
            } else {
                count += 2; // Two divisors: i and num/i
            }
        }
    }
    return count;
};

console.log("Optimized count for 30:", countDivisorsOptimized(30));
console.log("Optimized count for 36:", countDivisorsOptimized(36));
console.log("Optimized count for 25:", countDivisorsOptimized(25));
Optimized count for 30: 8
Optimized count for 36: 9
Optimized count for 25: 3

Method 3: Prime Factorization Approach

Using prime factorization, if a number has prime factors p?^a? × p?^a? × ... × p?^a?, then the number of divisors is (a?+1) × (a?+1) × ... × (a?+1).

const countDivisorsPrimeFactorization = (num) => {
    if (num === 1) return 1;
    
    let count = 1;
    let temp = num;
    
    // Check for factor 2
    let power = 0;
    while (temp % 2 === 0) {
        power++;
        temp /= 2;
    }
    if (power > 0) count *= (power + 1);
    
    // Check for odd factors starting from 3
    for (let i = 3; i * i <= temp; i += 2) {
        power = 0;
        while (temp % i === 0) {
            power++;
            temp /= i;
        }
        if (power > 0) count *= (power + 1);
    }
    
    // If temp is still greater than 1, it's a prime factor
    if (temp > 1) count *= 2;
    
    return count;
};

console.log("Prime factorization method for 30:", countDivisorsPrimeFactorization(30));
console.log("Prime factorization method for 60:", countDivisorsPrimeFactorization(60));
Prime factorization method for 30: 8
Prime factorization method for 60: 12

Performance Comparison

Method Time Complexity Best For
Simple Iteration O(n) Small numbers, easy to understand
Square Root O(?n) Medium numbers, good balance
Prime Factorization O(?n) Large numbers, most efficient

Conclusion

For counting divisors, the square root approach offers the best balance of simplicity and efficiency. The prime factorization method is most efficient for very large numbers, while simple iteration works well for educational purposes.

Updated on: 2026-03-15T23:19:00+05:30

631 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements