Getting Factors of a Number

前端 未结 11 2044
误落风尘
误落风尘 2020-12-30 11:53

I\'m trying to refactor this algorithm to make it faster. What would be the first refactoring here for speed?

public int GetHowManyFactors(int numberToCheck         


        
11条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 12:40

    Python Implementation Score 100% https://app.codility.com/demo/results/trainingJ78AK2-DZ5/

    
    import math;
    
    def solution(N):
        # write your code in Python 3.6
        NumberFactor=2; #one and the number itself
        if(N==1):
            return 1;
        if(N==2):
            return 2;
        squareN=int(math.sqrt(N)) +1;
        #print(squareN)
    
        for elem in range (2,squareN):
            if(N%elem==0):
                NumberFactor+=2;
        
        if( (squareN-1) * (squareN-1) ==N):
            NumberFactor-=1;
        return NumberFactor
    

提交回复
热议问题