Skip to content

Instantly share code, notes, and snippets.

@balazsnemeth
Created February 4, 2017 17:31
Show Gist options
  • Save balazsnemeth/d5928d45d635cf84b21cba6dcb52d39b to your computer and use it in GitHub Desktop.
Save balazsnemeth/d5928d45d635cf84b21cba6dcb52d39b to your computer and use it in GitHub Desktop.
// Codility - CountFactors
// Count factors of given number n.
// https://codility.com/demo/take-sample-test/count_factors/
function solution(N) {
// write your code in JavaScript (Node.js 6.4.0)
if (N <= 2) {
return N;
}
let max = Math.ceil(Math.sqrt(N));
//1 and N are always factors:
let numOfFactors = 2;
for (let i = 2; i < max; i++) {
let isFactor = N%i === 0;
numOfFactors += (isFactor ? 2 : 0)
}
numOfFactors += (N/max === max)
return numOfFactors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment