# Solution to Count-Factors by codility

28 Jul

Question Name: Count-Factors or CountFactors

### 11 Replies to “Solution to Count-Factors by codility”

1. Ignacio says:

Hi Sheng! Thanks for share your solutions!
In this one, I disagree, cause if N=1 the result should be 1 and in this solution return 0.
Just that!

• Sheng says:

I agree with your conclusion “if N=1 the result should be 1”, but disagree with “this solution return 0”. Actually this solution will return 1, if the input is 1. Please refer to the line #11.

2. tim ruff says:

When I run the C# version of your solution through Codility it fails timing on the last LARGE Number test case. As a result I modified it so that the multiplication isn`t performed for each loop in the while.

• Sheng says:

Nice improvement! Thanks!

3. Taku says:

Java solution

• Sheng says:

Thanks for sharing!

4. Gaston Tio says:

C# Solution – 100%

5. thomas says:

Result:
100/100/100
https://app.codility.com/demo/results/training97YW4B-RNP/

6. My initial approach. I treated differently odd and even numbers. Of course, one can use sqrt and avoiding additional operations. But improving that way, gave me no difference in codility tests.

7. michael brown says:

I used same solution, although in Java. Only got 93% with timeout for extremely large numbers, like MAXINT. Hard to see how to improve on 0(sqaureroot(N)) time.

8. Michael Brown says:

Actually I see now the reason for the timeout error on large numbers. The line 4 check, candidate * candidate,<N, will fail when candidate * candidate goes over MAXINT, in which case it turns negative, continuing the loop. You have to add a check for when this happens,