{"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

Solution in Javascript and Complexity O(sqrt(N))<\/strong><\/h2>\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

Here is the report<\/a> on Codility. Let me know if there’s a smarter way to solve this!<\/p>\n","protected":false},"excerpt":{"rendered":"

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. Solution in Javascript and Complexity O(sqrt(N)) Here is the report on Codility. Let me know if there’s a smarter way to solve this!<\/p>\n","protected":false},"author":1,"featured_media":429,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,5,6],"tags":[],"_links":{"self":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/428"}],"collection":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/comments?post=428"}],"version-history":[{"count":0,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/posts\/428\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media\/429"}],"wp:attachment":[{"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/media?parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/categories?post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danwritescode.com\/wp-json\/wp\/v2\/tags?post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}