Its Friday

Project Euler problem 40~

  1:     class Program
  2:     {
  3:         static void Main(string[] args)
  4:         {
  5:             Console.WriteLine("The value of the following expression:");
  6: 
  7:             TestBenchSetup();
  8:             TestBenchLoader(increasing_numbers_and_check_as_it_goes);
  9: 
 10:             Console.WriteLine("Press any key to exit");
 11:             Console.ReadKey();
 12:         }
 13: 
 14:         /// <summary>
 15:         /// pretty straight-forward:
 16:         /// there is no need to really create a concatenated number list, all we need is just the number and index
 17:         /// basically loop through the number and save the index at same time, so when the number is single
 18:         /// digits(1~9), increase index with length of the digit(1 atm), as number goes from 10,11,12...
 19:         /// increase the index by 2 for each iterated number, check to see if the index match the check_point(d1,d10,d100...)
 20:         /// however, since index may be multiple of 10s(since we are increasing the index by length of digits, which can be 3 for 
 21:         /// number like 453, and thats where the whole "if (index >= check_point)..." code comes to play
 22:         /// </summary>
 23:         /// <returns></returns>
 24:         static int increasing_numbers_and_check_as_it_goes()
 25:         {
 26:             var index = 1;
 27:             var num_length = 1;
 28:             var num = 1;
 29:             var tens = 10;
 30:             var result = 1;
 31:             var check_point = 1;
 32:             while (index <= 1000000)
 33:             {
 34:                 if (index >= check_point)
 35:                 {
 36:                     if (index == check_point)
 37:                         result *= Convert.ToByte(num.ToString()[0]) - 48;
 38:                     else
 39:                         result *= Convert.ToByte((num - 1).ToString()[num_length - (index - check_point)]) - 48;
 40: 
 41:                     check_point *= 10; //since the q asks for d1 * d10 * d100 * d1000....etc
 42:                 }
 43: 
 44:                 num++;
 45:                 index += num_length;
 46:                 if (num >= tens)
 47:                 {
 48:                     tens *= 10;
 49:                     num_length++;
 50:                 }
 51:             }
 52:             return result;
 53:         }
 54: 
 55:         static Stopwatch stopwatch = new Stopwatch();
 56:         static void TestBenchSetup()
 57:         {
 58:             // Uses the second Core or Processor for the Test
 59:             Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
 60:             // Prevents "Normal" processes from interrupting Threads
 61:             Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
 62:             // Prevents "Normal" Threads from interrupting this thread
 63:             Thread.CurrentThread.Priority = ThreadPriority.Highest;
 64:         }
 65:         // see http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx
 66:         static void TestBenchLoader(Func<int> test_method)
 67:         {
 68:             stopwatch.Reset();
 69:             stopwatch.Start();
 70:             var result = 0;
 71:             long avg_tick = 0;
 72:             long avg_ms = 0;
 73:             while (stopwatch.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 ms 
 74:             // stabilizes the CPU cache and pipeline.
 75:             {
 76:                 result = test_method(); // Warmup
 77:             }
 78:             stopwatch.Stop();
 79:             for (int repeat = 0; repeat < 20; ++repeat)
 80:             {
 81:                 stopwatch.Reset();
 82:                 stopwatch.Start();
 83:                 result = test_method();
 84:                 stopwatch.Stop();
 85:                 avg_tick += stopwatch.ElapsedTicks;
 86:                 avg_ms += stopwatch.ElapsedMilliseconds;
 87:             }
 88:             avg_tick = avg_tick / 20;
 89:             avg_ms = avg_ms / 20;
 90:             Console.WriteLine(string.Format("{0} way(ticks:{1}, ms:{2}) Ans:{3}",
 91:                 test_method.Method.Name.Replace('_', ' '), avg_tick, avg_ms, result));
 92:         }
 93:     }

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