{"id":419,"date":"2012-07-29T00:06:44","date_gmt":"2012-07-29T04:06:44","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=419"},"modified":"2012-09-07T16:18:40","modified_gmt":"2012-09-07T20:18:40","slug":"project-euler-problem-5","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/07\/29\/project-euler-problem-5\/","title":{"rendered":"Project Euler &#8211; Problem 5"},"content":{"rendered":"<p>Problem 5:<\/p>\n<p>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.<br \/>\nWhat is the smallest positive number that is\u00a0<b>evenly divisible<\/b>\u00a0by all of the numbers from 1 to 20?<br \/>\n<!--more--><br \/>\n<code><\/p>\n<pre lang=\"java\">\r\nimport java.util.*;\r\nimport java.lang.*;\r\n \r\nclass Main\r\n{\r\n        public static void main (String[] args) throws java.lang.Exception\r\n        {\r\n                Vector<Integer> arr = new Vector<Integer>();\r\n                for(int i=1;i<20;i++){\r\n                        Vector<Integer> tmp = factors(i);\r\n                        \/\/Merge the arrays\r\n                        int j = 0, k = 0;\r\n                        while(j < tmp.size() &#038;&#038; k < arr.size()){\r\n                                if(arr.elementAt(k).compareTo(tmp.elementAt(j)) == 0){\r\n                                        j++;k++;\r\n                                }else if(arr.elementAt(k).compareTo(tmp.elementAt(j)) > 0){\r\n                                        \/\/arr's is bigger than tmp's, so insert\r\n                                        arr.add(k, tmp.elementAt(j)); j++; k++;\r\n                                }else{\/\/tmp's is bigger than array's.\r\n                                        k++;\r\n                                }\r\n                        }\r\n                        \/\/append leftovers from tmp\r\n                        for(int m=j; m<tmp.size();m++){\r\n                                arr.add(tmp.elementAt(m));\r\n                        }\r\n                }\r\n                int prod = 1;\r\n                for(int m=0; m<arr.size();m++){\r\n                        prod *= arr.elementAt(m);\r\n                }\r\n                System.out.println(prod);\r\n        }\r\n \r\n        public static Vector<Integer> factors(int z){\r\n                Vector<Integer> result = new Vector<Integer>();\r\n \r\n                int i = 2;\r\n                while(z > 1){\r\n                        if(z % i == 0){\r\n                                z\/=i; result.add(new Integer(i));\r\n                        }else{\r\n                                i++;\r\n                        }\r\n                }\r\n \r\n                return result;\r\n        }\r\n \r\n}\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem 5:<\/p>\n<p>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.<br \/>\nWhat is the smallest positive number that is\u00a0<b>evenly divisible<\/b>\u00a0by all of the numbers from 1 to 20?<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[],"class_list":["post-419","post","type-post","status-publish","format-standard","hentry","category-project-euler"],"_links":{"self":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/comments?post=419"}],"version-history":[{"count":5,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":795,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/419\/revisions\/795"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}