Counts the number of divisors for a number - Java java.lang

Java examples for java.lang:Math Number

Description

Counts the number of divisors for a number

Demo Code


//package com.java2s;

public class Main {
    /**/*from w  w  w  .  jav a 2  s .  c o m*/
     * Counts the number of divisors for a number
     *
     * @param num Number to get the divisors for
     * @return The number of divisors for a number
     */
    public static long countDivisors(long num) {
        long count = 0;
        for (int i = 1; i <= Math.sqrt(num); i++) {
            if (num % i == 0)
                count += (num / i == i ? 1 : 2);
        }
        return count;
    }
}

Related Tutorials