Created
February 4, 2017 17:31
-
-
Save balazsnemeth/d5928d45d635cf84b21cba6dcb52d39b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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