blah

Project Euler problem 91~
c#

  1:     class Program
  2:     {
  3:         static void Main(string[] args)
  4:         {
  5:             Console.WriteLine("The numbers of right triangles can be formed: ");
  6: 
  7:             TestBenchSetup();
  8:             TestBenchLoader(plain_brute_force_find_and_test_all_triangles);
  9: 
 10:             Console.WriteLine("Press any key to exit");
 11:             Console.ReadKey();
 12:         }
 13: 
 14:         /// <summary>
 15:         /// basically iterate through all the points and use Pythagorean theorem to test if formed triangle is a
 16:         /// right triangle
 17:         /// </summary>
 18:         static int plain_brute_force_find_and_test_all_triangles()
 19:         {
 20:             int x1, y1, x2, y2;
 21:             var x0 = 0;
 22:             var y0 = 0;
 23:             var limit = 50;
 24:             var counter = 0;
 25:             for (x1 = x0; x1 <= limit; x1++)
 26:             {
 27:                 for (y1 = y0; y1 <= limit; y1++)
 28:                 {
 29:                     for (x2 = x0; x2 <= limit; x2++)
 30:                     {
 31:                         for (y2 = y0; y2 <= limit; y2++)
 32:                         {
 33:                             var p0p1 = x1 * x1 + y1 * y1;
 34:                             var p0p2 = x2 * x2 + y2 * y2;
 35:                             var x_diff = x2 - x1;
 36:                             var y_diff = y2 - y1;
 37:                             var p1p2 = x_diff * x_diff + y_diff * y_diff;
 38:                             var c = p1p2;
 39:                             var b = p0p1;
 40:                             var a = p0p2;
 41:                             if (c < p0p1)
 42:                             {
 43:                                 c = p0p1;
 44:                                 b = p1p2;
 45:                                 a = p0p2;
 46:                             }
 47:                             if (c < p0p2)
 48:                             {
 49:                                 c = p0p2;
 50:                                 b = p1p2;
 51:                                 a = p0p1;
 52:                             }
 53:                             if (b != 0 && a != 0 && a + b == c)
 54:                                 counter++;
 55:                         }
 56:                     }
 57:                 }
 58:             }
 59:             return counter / 2; // remove duplicates
 60:         }
 61: 
 62:         static Stopwatch stopwatch = new Stopwatch();
 63:         static void TestBenchSetup()
 64:         {
 65:             // Uses the second Core or Processor for the Test
 66:             Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
 67:             // Prevents "Normal" processes from interrupting Threads
 68:             Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
 69:             // Prevents "Normal" Threads from interrupting this thread
 70:             Thread.CurrentThread.Priority = ThreadPriority.Highest;
 71:         }
 72:         // see http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx
 73:         static void TestBenchLoader(Func<int> test_method)
 74:         {
 75:             stopwatch.Reset();
 76:             stopwatch.Start();
 77:             var result = 0;
 78:             long avg_tick = 0;
 79:             long avg_ms = 0;
 80:             while (stopwatch.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 ms 
 81:             // stabilizes the CPU cache and pipeline.
 82:             {
 83:                 result = test_method(); // Warmup
 84:             }
 85:             stopwatch.Stop();
 86:             for (int repeat = 0; repeat < 20; ++repeat)
 87:             {
 88:                 stopwatch.Reset();
 89:                 stopwatch.Start();
 90:                 result = test_method();
 91:                 stopwatch.Stop();
 92:                 avg_tick += stopwatch.ElapsedTicks;
 93:                 avg_ms += stopwatch.ElapsedMilliseconds;
 94:             }
 95:             avg_tick = avg_tick / 20;
 96:             avg_ms = avg_ms / 20;
 97:             Console.WriteLine(string.Format("{0} way(ticks:{1}, ms:{2}) Ans:{3}",
 98:                 test_method.Method.Name.Replace('_', ' '), avg_tick, avg_ms, result));
 99:         }
100:     }

c++

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