Codility 'Count Factors' Solution

Martin Kysel · July 28, 2014

Short Problem Definition:

Count factors of given number N.

CountFactors

Complexity:

expected worst-case time complexity is O(sqrt(N))

expected worst-case space complexity is O(1).

Execution:

This example can be found in the lesson document.

Solution:

def solution(N):
    cnt = 0
    i = 1
    while ( i * i <= N):
        if (N % i == 0):
            if i * i == N:
               cnt += 1
            else:
                cnt += 2
        i += 1
    return cnt

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.