{"id":474,"date":"2012-07-30T16:47:58","date_gmt":"2012-07-30T20:47:58","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=474"},"modified":"2012-09-07T16:16:23","modified_gmt":"2012-09-07T20:16:23","slug":"project-euler-problem-19","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/07\/30\/project-euler-problem-19\/","title":{"rendered":"Project Euler &#8211; Problem 19"},"content":{"rendered":"<p>Problem 19: How many Sundays fell on the first of the month during the twentieth century?<br \/>\n<!--more--><br \/>\n<code><\/p>\n<pre lang=\"java\">\r\npublic class Main{\r\n \r\n        private static boolean isLeap(int year){\r\n                if( year % 400 == 0 ) return true;\r\n                if( year % 4 == 0 && year % 100 != 0 ) return true;\r\n                return false;\r\n        }\r\n        \r\n        public static void main(String[] args) {\r\n                int year = 1901; int year_end = 2000;\r\n                int find_day = 6; \/\/Find Sunday (0 is Monday)\r\n                \r\n                int[] nonleap = {31,28,31,30,31,30,31,31,30,31,30,31};\r\n                int[] leap = {31,29,31,30,31,30,31,31,30,31,30,31};\r\n                int counter = 0;\r\n                \r\n                \/*int day = 0;  which is Jan 1 1900, Monday\r\n                        setup the date - (the day before \"var year\") *\/\r\n                int setup_year = 1900;\r\n                int day = 0;\r\n                while(setup_year < year){\r\n                        int[] months = isLeap(year) ? leap : nonleap;\r\n                        for(int month : months){\r\n                                day += month;\r\n                        }\r\n                        setup_year ++;\r\n                }\r\n \r\n                while(year <= year_end){\r\n                        int[] months = isLeap(year) ? leap : nonleap;\r\n                        for(int month : months){\r\n                                if((day+1)% 7 == find_day) counter ++;\/\/Find first \"day\" of each month\r\n                                day += month;\r\n                        }\r\n                        year ++;\r\n                }\r\n                System.out.println(counter);\r\n        }\r\n}\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem 19: How many Sundays fell on the first of the month during the twentieth century?<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[56],"tags":[],"_links":{"self":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/474"}],"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=474"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/474\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}