{"id":1054,"date":"2012-05-11T17:05:30","date_gmt":"2012-05-11T23:05:30","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1054"},"modified":"2018-07-01T23:06:00","modified_gmt":"2018-07-02T05:06:00","slug":"code-of-the-day-javascript-prime-factors-of-a-number","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/05\/code-of-the-day-javascript-prime-factors-of-a-number\/","title":{"rendered":"Code of the Day: Javascript, Prime Factors of a Number"},"content":{"rendered":"<p><b>Javascript: Prime Factorization<\/b><br \/>\n<a href=\"http:\/\/www.flickr.com\/photos\/christmasnotebook\/5802829765\/\"><img decoding=\"async\" src=\"http:\/\/farm4.staticflickr.com\/3204\/5802829765_b86a6fc03c.jpg\"><\/a><br \/>\nToday&#8217;s code is a enhancement of <a href=\"http:\/\/www.coderenaissance.com\/2011\/06\/finding-prime-factors-in-javascript.html\">Code Renaissance<\/a>&#8216;s version of &#8220;Finding Prime Numbers in Javascript&#8221;.<br \/>\nMain difference are the following.<\/p>\n<ol>\n<li>Faster performance by eliminating recursion and caching Math.sqrt.<\/li>\n<li>Whole numbers bigger than 1 return an empty array, since they&#8217;re not prime numbers.<\/li>\n<li>Decimal values are converted to whole numbers.<\/li>\n<\/ol>\n<p><code><\/p>\n<pre lang='javascript'>\r\n\/**\r\n* @author Larry Battle - http:\/\/bateru.com\/news\/contact-me\r\n* @date May 11, 2012\r\n* @license MIT and GPL v3\r\n* @purpose Return the prime factors of a number.\r\n* @info - http:\/\/bateru.com\/news\/?s=prime+factors\r\n*\/\r\nvar getPrimeFactors = function(num) {\r\n    num = Math.floor(num);\r\n    var root, factors = [], x, sqrt = Math.sqrt, doLoop = 1 < num;\r\n    while( doLoop ){\r\n        root = sqrt(num);\r\n        x = 2;\r\n        if (num % x) {\r\n            x = 3;\r\n            while ((num % x) &#038;&#038; ((x += 2) < root));\r\n        }\r\n        x = (x > root) ? num : x;\r\n        factors.push(x);\r\n        doLoop = ( x != num );\r\n        num \/= x;\r\n    }\r\n    return factors;\r\n}\r\n<\/pre>\n<p><\/code><\/p>\n<p><b>Example:<\/b><br \/>\ngetPrimeFactors(15120) \/\/ returns [2, 2, 2, 3, 3, 3, 7]<\/p>\n<p><b>Demo:<\/b><br \/>\n<iframe style=\"width: 100%; height: 300px\" src=\"https:\/\/jsfiddle.net\/tWyHg\/43\/embedded\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Javascript: Prime Factorization Today&#8217;s code is a enhancement of Code Renaissance&#8216;s version of &#8220;Finding Prime Numbers in Javascript&#8221;. Main difference are the following. Faster performance by eliminating recursion and caching Math.sqrt. Whole numbers bigger than 1 return an empty array, since they&#8217;re not prime numbers. Decimal values are converted to whole numbers. \/** * @author &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/05\/code-of-the-day-javascript-prime-factors-of-a-number\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Javascript, Prime Factors of a Number<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,10],"tags":[30,164,166,141,138],"class_list":["post-1054","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-tutorials","tag-code-of-the-day","tag-javascript","tag-math","tag-prime-factorization","tag-prime-factors"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1054"}],"version-history":[{"count":11,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1054\/revisions"}],"predecessor-version":[{"id":1580,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1054\/revisions\/1580"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}