not much

Project Euler problem 30~

  1:     class Program
  2:     {
  3:         static void Main(string[] args)
  4:         {
  5:             Console.WriteLine("The sum of all the numbers that can be written as the sum of fifth powers of their digits:");
  6: 
  7:             TestBenchSetup();
  8:             TestBenchLoader(loop_through_all_number_in_range_to_find_possible_match);
  9: 
 10:             Console.WriteLine("Press any key to exit");
 11:             Console.ReadKey();
 12:         }
 13: 
 14:         /// <summary>
 15:         /// the tricky part here is to find the range, the lower range we can just make it 2
 16:         /// the upper is trickier. the number itself seems to advance a lot faster than sum of
 17:         /// digits in their respective power.
 18:         /// for number with 5 digits: min:10000  max:99999  min(digitpower):1 max(digitpower)=(9^5)*5=295245
 19:         /// for number with 6 digits: min:100000 max:999999 min(digitpower):1 max(digitpower)=(9^5)*6=354294
 20:         /// since the smallest number in 6 digits(100000) is far greater than sum of max digit in power 6
 21:         /// so its impossible for number with 6 digits or greater that might have matching sum of digits in power
 22:         /// the upper range must lies within number with 5 digits and since max digit power is (9^5)*5=295245
 23:         /// thus it is the upper range
 24:         /// </summary>
 25:         /// <returns></returns>
 26:         static int loop_through_all_number_in_range_to_find_possible_match()
 27:         {
 28:             var power_map = Enumerable.Range(0, 10).Select(l => (int)Math.Pow(l, 5)).ToArray();
 29:             var max = power_map[9] * 5;
 30:             var sum = 0;
 31:             var num = 0;
 32:             var rem = 0;
 33:             var total = 0;
 34:             for (int i = 2; i <= max; i++)
 35:             {
 36:                 sum = 0;
 37:                 num = i;
 38:                 while (num > 0)
 39:                 {
 40:                     rem = num % 10;
 41:                     num = num / 10;
 42:                     sum += power_map[rem];
 43:                 }
 44:                 if (i == sum)
 45:                     total += sum;
 46:             }
 47:             return total;
 48:         }
 49: 
 50:         static Stopwatch stopwatch = new Stopwatch();
 51:         static void TestBenchSetup()
 52:         {
 53:             // Uses the second Core or Processor for the Test
 54:             Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
 55:             // Prevents "Normal" processes from interrupting Threads
 56:             Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
 57:             // Prevents "Normal" Threads from interrupting this thread
 58:             Thread.CurrentThread.Priority = ThreadPriority.Highest;
 59:         }
 60:         // see http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx
 61:         static void TestBenchLoader(Func<int> test_method)
 62:         {
 63:             stopwatch.Reset();
 64:             stopwatch.Start();
 65:             var result = 0;
 66:             long avg_tick = 0;
 67:             long avg_ms = 0;
 68:             while (stopwatch.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 ms 
 69:             // stabilizes the CPU cache and pipeline.
 70:             {
 71:                 result = test_method(); // Warmup
 72:             }
 73:             stopwatch.Stop();
 74:             for (int repeat = 0; repeat < 20; ++repeat)
 75:             {
 76:                 stopwatch.Reset();
 77:                 stopwatch.Start();
 78:                 result = test_method();
 79:                 stopwatch.Stop();
 80:                 avg_tick += stopwatch.ElapsedTicks;
 81:                 avg_ms += stopwatch.ElapsedMilliseconds;
 82:             }
 83:             avg_tick = avg_tick / 20;
 84:             avg_ms = avg_ms / 20;
 85:             Console.WriteLine(string.Format("{0} way(ticks:{1}, ms:{2}) Ans:{3}",
 86:                 test_method.Method.Name.Replace('_', ' '), avg_tick, avg_ms, result));
 87:         }
 88:     }

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