Given n integer, your task is to find the number of divisors of that given number.
Example:
Input: n = 16
Output: 5
Approach
Java
public class CountingDivisors {public static void main(String[] args) {int n = 16;System.out.println(countingDivisors(n));}static int countingDivisors(int n) {int ans = 0;for (int i = 1; i * i <= n; i++) {if (n % i == 0) {ans += 2;if (i * i == n)ans--;}}return ans;}}
C++
#include <bits/stdc++.h>using namespace std;int countingDivisors(int n){int ans = 0;for (int i = 1; i * i <= n; i++){if (n % i == 0){ans += 2;if (i * i == n)ans--;}}return ans;}int main(){int n = 16;cout << countingDivisors(n) << "\n";return 0;}
No comments:
Post a Comment