{"id":428,"date":"2020-03-26T06:16:00","date_gmt":"2020-03-26T06:16:00","guid":{"rendered":"https:\/\/danwritescode.com\/?p=428"},"modified":"2020-06-26T06:22:31","modified_gmt":"2020-06-26T06:22:31","slug":"count-factors-codility-100-correct-javascript-solution","status":"publish","type":"post","link":"https:\/\/danwritescode.com\/count-factors-codility-100-correct-javascript-solution\/","title":{"rendered":"Count Factors – Codility 100% Correct Javascript Solution"},"content":{"rendered":"\n
The Count Factors challenge is just the classic finding the division factors of a number. The most efficient way I found was iterating until the square root of the number and subsequently dividing the number by all the factors we find.<\/p>\n\n\n\n
function solution(N) {\n if (N===1) return 1;\n let multiplier = 1;\n let result = 1;\n \/\/can be even more quick if we take out the 2s\n for (let i = 2; i <=Math.sqrt(N) ; i++) {\n if (N % i === 0) {\n multiplier = 1\n while (N % i === 0) {\n N \/= i\n multiplier++\n }\n result *= multiplier\n }\n }\n \/\/if it is a prime number, we add another factor of 2\n if (N !== 1) {result *=2}\n return result\n}<\/code><\/pre>\n\n\n\n