# CountFactors – Codility – Solution

Java solution to Codility CountFactors problem (Lesson 10 – Prime and composite numbers) which scored 100%. The problem is to count all the factors of a given integer N.

Note: using a brute force approach (checking every number below N) wouldn’t pass this problem because it would take too long.

The strategy is to find all factors by only checking up to square root of N and incrementing by 2 each time N % i == 0.

```
12345678910111213141516171819202122package com.codility.lesson10.primecomposite;

public class CountFactors {
public int solution(int N) {
int factors = 0;
int squareRootN = (int) Math.sqrt(N);
if(Math.pow(squareRootN, 2) != N) {
squareRootN++; //round up for any non-perfect squares
}
else { //perfect squares have an additional factor
factors++;
}

for(int i=1; i<squareRootN; i++) {
if(N % i == 0) {
factors += 2;
}
}

return factors;
}
}

```

TestNG test cases for this problem which all passed:

```
1234567891011121314151617181920212223242526272829303132333435363738package test.com.codility.lesson10.primecomposite;

import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.codility.lesson10.primecomposite.CountFactors;

public class CountFactorsTests {
private CountFactors solution;

@BeforeTest
public void setUp() {
solution = new CountFactors();
}

@DataProvider(name = "test1")
public Object [][] createData1() {
return new Object [][] {
new Object [] {   1, 1 },
new Object [] {   2, 2 }, //1, 2
new Object [] {   3, 2 }, //1, 3
new Object [] {   4, 3 }, //1, 2, 4
new Object [] {  10, 4 }, //1, 2, 5, 10
new Object [] {  24, 8 }, //1, 2, 3, 4, 6, 8, 12, 24
new Object [] {  25, 3 }, //1, 5, 25
new Object [] {  36, 9 }, //1, 2, 3, 4, 6, 9, 12, 18, 36
new Object [] {  56, 8 }, //1, 2, 4, 7, 8, 14, 28, 56
new Object [] { 100, 9 }, //1, 2, 4, 5, 10, 20, 25, 50, 100
};
}

@Test(dataProvider = "test1")
public void verifySolution(int pA, int pExpected) {
Assert.assertEquals(solution.solution(pA), pExpected);
}
}

```