2017年2月26日 星期日

【Python】Codility in Python : Lesson 10 - Prime and composite numbers【CountFactors】

Prime and composite numbers 第一題:【CountFactors】
Count factors of given number n.

A positive integer D is a factor of a positive integer N
if there exists an integer M such that N = D * M.

For example, 6 is a factor of 24, because M = 4 satisfies the above condition (24 = 6 * 4).

Write a function:

  def solution(N)

that, given a positive integer N, returns the number of its factors.

For example, given N = 24, the function should return 8,
because 24 has 8 factors, namely 1, 2, 3, 4, 6, 8, 12, 24. There are no other factors of 24.

Assume that:
  N is an integer within the range [1..2,147,483,647].


方法一:用平方根的方式推算
Correctness:100%、Performance:100%
```python import math def solutionBySqrt(N): result = 0 squareRoot = math.sqrt(N) for index in range(1, math.ceil(squareRoot)): if N % index == 0: result += 2 if squareRoot == math.ceil(squareRoot): result += 1 return result ```

方法二:用 lambda 寫法一行解決,可惜效能太差 ...
Correctness:100%、Performance:0%
```python def solutionByLambda(N): return len(list(filter(lambda x: N % x == 0, range(1, N+1)))) ```

完整練習題 source code 請參閱:github

沒有留言:

張貼留言