Solved Problem - Factorization
Explore how to count the number of factors of a given number efficiently by applying mathematical insights that reduce complexity from linear to square root time. Understand factor pairs and optimization strategies that enable faster computation for large values, preparing you for more advanced number theory problems.
We'll cover the following...
Problem statement
Given a number , count the number of factors of the number N.
Input format
A single line of input contains the number .
Output format
Print a single integer equal to the number of factors of .
Sample
Input
36
Output
9
Explanation
Factors of
Count:
Brute force
The brute force solution would be to loop over all the numbers from 1 to N and check if it is a factor. If it is, you would then print it.
We can use the modulus operator to check if it’s a factor or not.
Here is the code:
Since there is only one loop that runs N times, the runtime complexity is simply ...