Skip to content

Commit dfd0423

Browse files
committed
Added fastest way to compute number of divisors of a number
1 parent 259203f commit dfd0423

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class ComputeNumberOfDivisors {
2+
3+
// Function that calculates number of divisors
4+
// Time Complexity : O( (n)^ 1/2 )
5+
static int numberOfDivisors(int n) {
6+
int count = 0;
7+
for (int i = 1; i <= Math.sqrt(n); i++) {
8+
if (n % i == 0) {
9+
if (n / i == i) {
10+
count++;
11+
} else {
12+
count += 2;
13+
}
14+
}
15+
}
16+
return count;
17+
}
18+
19+
public static void main(String[] args) {
20+
int x = 25;
21+
System.out.println("Total number of divisor of " + x + " are " + numberOfDivisors(x));
22+
}
23+
}

0 commit comments

Comments
 (0)