{"id":102,"date":"2012-10-21T06:41:16","date_gmt":"2012-10-21T10:41:16","guid":{"rendered":"http:\/\/www.ferociouscoder.com\/blog\/?p=102"},"modified":"2013-07-13T02:50:32","modified_gmt":"2013-07-13T06:50:32","slug":"factors","status":"publish","type":"post","link":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html","title":{"rendered":"Factors"},"content":{"rendered":"<p>A naive way of figuring out the number of factors of a particular number is to try every number from 1 up to the number itself.<\/p>\n<pre>\r\npublic ArrayList getFactors(int N) {\r\n  ArrayList factors = new ArrayList();\r\n  for (int i=0; i if (N%i==0) {\r\n      factors.add(i);\r\n  }\r\n  return factors;\r\n}\r\n<\/pre>\n<p>We can do much better than this by noticing that for every number i that is a factor of N; the number N\/i is also a factor of N! This means that we can change out O(N) algorithm to O(sqrt(N)) which is much much faster.<\/p>\n<figure id=\"attachment_103\" aria-describedby=\"caption-attachment-103\" style=\"width: 500px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/i0.wp.com\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"103\" data-permalink=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html\/graph\" data-orig-file=\"https:\/\/i0.wp.com\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png?fit=500%2C500&amp;ssl=1\" data-orig-size=\"500,500\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"F(x) vs F(sqrt(X))\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png?fit=474%2C474&amp;ssl=1\" class=\"size-full wp-image-103 \" title=\"F(x) vs F(sqrt(X))\" src=\"https:\/\/i0.wp.com\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png?resize=474%2C474&#038;ssl=1\" alt=\"F(x) vs F(sqrt(X))\" width=\"474\" height=\"474\" srcset=\"https:\/\/i0.wp.com\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png?w=500&amp;ssl=1 500w, https:\/\/i0.wp.com\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png?resize=300%2C300&amp;ssl=1 300w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/a><figcaption id=\"caption-attachment-103\" class=\"wp-caption-text\">O(N) algorithm vs O(sqrt(N)) algorithm<\/figcaption><\/figure>\n<pre>\r\npublic ArrayList getFactors(int N) {\r\n  ArrayList factors = new ArrayList();\r\n  int sqrt = (int) Math.sqrt(N);\r\n  for (int i=0; i if (N%i==0) {\r\n    factors.add(i);\r\n  }\r\n  if (sqrt * sqrt == N) {\r\n    factors.add(sqrt);\r\n  }\r\n  return factors;\r\n}\r\n<\/pre>\n<p>Save this code somewhere, I guarantee it&#8217;ll come in handy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A naive way of figuring out the number of factors of a particular number is to try every number from 1 up to the number itself. public ArrayList getFactors(int N) { ArrayList factors = new ArrayList(); for (int i=0; i if (N%i==0) { factors.add(i); } return factors; } We can do much better than this &hellip; <a href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Factors<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[14,19],"tags":[23,20],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-java","tag-algorithms-2","tag-java-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Factors - Ferocious Coder<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Factors - Ferocious Coder\" \/>\n<meta property=\"og:description\" content=\"A naive way of figuring out the number of factors of a particular number is to try every number from 1 up to the number itself. public ArrayList getFactors(int N) { ArrayList factors = new ArrayList(); for (int i=0; i if (N%i==0) { factors.add(i); } return factors; } We can do much better than this &hellip; Continue reading Factors &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html\" \/>\n<meta property=\"og:site_name\" content=\"Ferocious Coder\" \/>\n<meta property=\"article:published_time\" content=\"2012-10-21T10:41:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-07-13T06:50:32+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png\" \/>\n<meta name=\"author\" content=\"Rawrosaur\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rawrosaur\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html\"},\"author\":{\"name\":\"Rawrosaur\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"headline\":\"Factors\",\"datePublished\":\"2012-10-21T10:41:16+00:00\",\"dateModified\":\"2013-07-13T06:50:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html\"},\"wordCount\":101,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"image\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/graph.png\",\"keywords\":[\"algorithms\",\"java\"],\"articleSection\":[\"Algorithms\",\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html\",\"name\":\"Factors - Ferocious Coder\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/graph.png\",\"datePublished\":\"2012-10-21T10:41:16+00:00\",\"dateModified\":\"2013-07-13T06:50:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html#primaryimage\",\"url\":\"http:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/graph.png\",\"contentUrl\":\"http:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/graph.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/factors-102.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Factors\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/\",\"name\":\"Ferocious Coder\",\"description\":\"RAWR!\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\",\"name\":\"Rawrosaur\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"caption\":\"Rawrosaur\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\"},\"sameAs\":[\"http:\\\/\\\/www.ferociouscoder.com\\\/\"],\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/author\\\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Factors - Ferocious Coder","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html","og_locale":"en_US","og_type":"article","og_title":"Factors - Ferocious Coder","og_description":"A naive way of figuring out the number of factors of a particular number is to try every number from 1 up to the number itself. public ArrayList getFactors(int N) { ArrayList factors = new ArrayList(); for (int i=0; i if (N%i==0) { factors.add(i); } return factors; } We can do much better than this &hellip; Continue reading Factors &rarr;","og_url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html","og_site_name":"Ferocious Coder","article_published_time":"2012-10-21T10:41:16+00:00","article_modified_time":"2013-07-13T06:50:32+00:00","og_image":[{"url":"http:\/\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png","type":"","width":"","height":""}],"author":"Rawrosaur","twitter_misc":{"Written by":"Rawrosaur","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html#article","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html"},"author":{"name":"Rawrosaur","@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"headline":"Factors","datePublished":"2012-10-21T10:41:16+00:00","dateModified":"2013-07-13T06:50:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html"},"wordCount":101,"commentCount":0,"publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"image":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html#primaryimage"},"thumbnailUrl":"http:\/\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png","keywords":["algorithms","java"],"articleSection":["Algorithms","Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html","url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html","name":"Factors - Ferocious Coder","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html#primaryimage"},"image":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html#primaryimage"},"thumbnailUrl":"http:\/\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png","datePublished":"2012-10-21T10:41:16+00:00","dateModified":"2013-07-13T06:50:32+00:00","breadcrumb":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html#primaryimage","url":"http:\/\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png","contentUrl":"http:\/\/www.ferociouscoder.com\/blog\/wp-content\/uploads\/2012\/10\/graph.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/factors-102.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.ferociouscoder.com\/blog"},{"@type":"ListItem","position":2,"name":"Factors"}]},{"@type":"WebSite","@id":"https:\/\/www.ferociouscoder.com\/blog\/#website","url":"https:\/\/www.ferociouscoder.com\/blog\/","name":"Ferocious Coder","description":"RAWR!","publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.ferociouscoder.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a","name":"Rawrosaur","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","caption":"Rawrosaur"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg"},"sameAs":["http:\/\/www.ferociouscoder.com\/"],"url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/author\/admin"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s1mYMV-factors","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":8,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":314,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions\/314"}],"wp:attachment":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}