not much

Project Euler problem 19~

  1:     class Program
  2:     {
  3:         static void Main(string[] args)
  4:         {
  5:             var sw = new Stopwatch();
  6: 
  7:             Console.WriteLine("The number of Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)");
  8:             sw.Start();
  9:             var sum1 = test();
 10:             sw.Stop();
 11:             Console.WriteLine(string.Format("List<DateUnit> and LINQ filter way(tick:{0}): {1}", sw.ElapsedTicks, sum1));
 12: 
 13:             sw.Reset();
 14: 
 15:             sw.Start();
 16:             var sum2 = test2();
 17:             sw.Stop();
 18:             Console.WriteLine(string.Format("plain iterate through way(tick:{0}): {1}", sw.ElapsedTicks, sum2));
 19: 
 20:             sw.Reset();
 21: 
 22:             sw.Start();
 23:             var sum3 = test3();
 24:             sw.Stop();
 25:             Console.WriteLine(string.Format("LINQ + BCL DateTime Object(tick:{0}): {1}", sw.ElapsedTicks, sum3));
 26: 
 27:             Console.WriteLine("Press any key to exit");
 28:             Console.ReadKey();
 29:         }
 30: 
 31:         [DebuggerDisplay("{Month}/{Day}/{Year}/{DayOfWeek}")]
 32:         public class DateUnit
 33:         {
 34:             public int Year { get; set; }
 35:             public int Month { get; set; }
 36:             public int Day { get; set; }
 37:             public int DayOfWeek { get; set; }
 38:             public DateUnit(int year, int month, int date, int day)
 39:             {
 40:                 Year = year;
 41:                 Month = month;
 42:                 Day = date;
 43:                 DayOfWeek = day;
 44:             }
 45:         }
 46: 
 47:         static int test()
 48:         {
 49:             var cal = new List<DateUnit>();
 50:             // initial setting
 51:             var year = 1900;
 52:             var month = 1;
 53:             var day = 1;
 54:             var day_of_week = 0; // monday = 0, tuesday=1,..... sunday =6
 55:             var month_counter = 0;
 56: 
 57:             while (true)
 58:             {
 59:                 if (month == 4 || month == 6 || month == 9 || month == 11)
 60:                     month_counter = 30;
 61:                 else if (month == 2)
 62:                     month_counter = year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 : 28;
 63:                 else
 64:                     month_counter = 31;
 65: 
 66:                 for (day = 1; day <= month_counter; day++)
 67:                 {
 68:                     cal.Add(new DateUnit(year, month, day, day_of_week % 7));
 69:                     day_of_week++;
 70:                 }
 71: 
 72:                 year = month == 12 ? year + 1 : year;
 73:                 month = month == 12 ? 1 : month + 1;
 74: 
 75:                 if (year == 2001)
 76:                     break;
 77:             }
 78:             return cal.Where(l => l.Year >= 1901 && l.Day == 1 && l.DayOfWeek == 6).Count();
 79:         }
 80: 
 81:         static int test2()
 82:         {
 83:             // initial setting
 84:             var year = 1900;
 85:             var month = 1;
 86:             var day = 1;
 87:             var day_of_week = 0; // monday = 0, tuesday=1,..... sunday =6
 88:             var count = 0;
 89:             var month_counter = 0;
 90: 
 91:             while (true)
 92:             {
 93:                 month_counter = month == 4 || month == 6 || month == 9 || month == 11 ?
 94:                     30 : month == 2 ? year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 : 28 : 31;
 95: 
 96:                 for (day = 1; day <= month_counter; day++)
 97:                 {
 98:                     if (year >= 1901 && day == 1 && day_of_week % 7 == 6)
 99:                         count++;
100: 
101:                     day_of_week++;
102:                 }
103: 
104:                 if (month == 12)
105:                 {
106:                     month = 1;
107:                     year++;
108: 
109:                     if (year == 2001)
110:                         break;
111:                 }
112:                 else
113:                     month++;
114:             }
115: 
116:             return count;
117:         }
118: 
119:         static int test3()
120:         {
121:             var start = new DateTime(1901, 1, 1);
122:             var end = new DateTime(2000, 12, 31);
123:             return Enumerable.Range(0, end.Subtract(start).Days)
124:                 .Select(l => start.AddDays(l))
125:                 .Where(l => l.Day == 1 && l.DayOfWeek == DayOfWeek.Sunday)
126:                 .Count();
127:         }
128: 
129:         static int test4()
130:         {
131:             var day_of_week = 0; // monday = 0, tuesday=1,..... sunday =6
132:             var count = 0;
133:             var days_in_month = new int[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
134: 
135:             for (int year = 1900; year <= 2000; year++)
136:             {
137:                 days_in_month[2] = year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 : 28;
138:                 for (int month = 1; month <= 12; month++)
139:                 {
140:                     for (int day = 1; day <= days_in_month[month]; day++)
141:                     {
142:                         if (year >= 1901 && day == 1 && day_of_week % 7 == 6)
143:                             count++;
144: 
145:                         day_of_week++;
146:                     }
147:                 }
148:             }
149: 
150:             return count;
151:         }
152:        
153:     }

Advertisements
This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s