easy
0 views

Factor Count – CTS PATTERN

Count the number of positive divisors (factors) of a given integer.

Understand the Problem

Problem Statement

You are given an integer num. Your task is to determine how many positive integers divide num without leaving a remainder. This count is known as the factor count or divisor count of the number.

Function Signature:

  • int getFactorCount(int num)

The function accepts a single integer num as input and should return an integer representing the total number of its positive factors.

Constraints

  • 1 ≤ num ≤ 106
  • The input num is a positive integer.

Examples

Example 1
Input
12
Output
6
Explanation

The factors of 12 are 1, 2, 3, 4, 6, and 12. Therefore, the factor count is 6.

Example 2
Input
7
Output
2
Explanation

The factors of 7 are 1 and 7. Since 7 is a prime number, it has exactly 2 factors.

Example 3
Input
1
Output
1
Explanation

The only factor of 1 is 1 itself. Therefore, the factor count is 1.

Solution

#include <stdio.h>

int getFactorCount(int num){
    int count = 0;
    for (int i = 1; i <= num; i++) {
        if (num % i == 0) {
            count++;
        }
    }
    return count;
}

int main() {
    int num;
    scanf("%d", &num);
    printf("%d\n", getFactorCount(num));
    return 0;
}
Time:O(n)
Space:O(1)
Approach:

C Implementation:

  1. Define the function getFactorCount that takes an integer parameter num.
  2. Initialize a variable count to 0 to keep track of the number of factors.
  3. Use a for loop to iterate from 1 to num (inclusive).
  4. Inside the loop, check if num is divisible by the current iterator i using the modulo operator (%).
  5. If the condition is true, increment the count.
  6. After the loop, return the final count.
  7. In the main function, read the input, call getFactorCount, and print the result.

Time Complexity: O(n) Space Complexity: O(1)

Visual Explanation

Loading diagram...