xaml~

Project Euler problem 8~

  1:  class Program
  2:     {
  3:         static readonly char[] number = @"73167176531330624919225119674426574742355349194934
  4:                                           96983520312774506326239578318016984801869478851843
  5:                                           85861560789112949495459501737958331952853208805511
  6:                                           12540698747158523863050715693290963295227443043557
  7:                                           66896648950445244523161731856403098711121722383113
  8:                                           62229893423380308135336276614282806444486645238749
  9:                                           30358907296290491560440772390713810515859307960866
 10:                                           70172427121883998797908792274921901699720888093776
 11:                                           65727333001053367881220235421809751254540594752243
 12:                                           52584907711670556013604839586446706324415722155397
 13:                                           53697817977846174064955149290862569321978468622482
 14:                                           83972241375657056057490261407972968652414535100474
 15:                                           82166370484403199890008895243450658541227588666881
 16:                                           16427171479924442928230863465674813919123162824586
 17:                                           17866458359124566529476545682848912883142607690042
 18:                                           24219022671055626321111109370544217506941658960408
 19:                                           07198403850962455444362981230987879927244284909188
 20:                                           84580156166097919133875499200524063689912560717606
 21:                                           05886116467109405077541002256983155200055935729725
 22:                                           71636269561882670428252483600823257530420752963450".Replace(" ","")
 23:                                                                                              .Replace(Environment.NewLine, "")
 24:                                                                                              .ToCharArray();
 25: 
 26:         static void Main(string[] args)
 27:         {
 28:             var sw = new Stopwatch();
 29: 
 30:             Console.WriteLine("The greatest product of five consecutive digits in the 1000-digit number");
 31:             sw.Start();
 32:             var product1 = BruteForce();
 33:             sw.Stop();
 34:             Console.WriteLine(string.Format("plain loop and compare brute force way(tick:{0}): {1}", sw.ElapsedTicks, product1));
 35:             sw.Reset();
 36: 
 37:             Console.WriteLine("Press any key to exit");
 38:             Console.ReadKey();
 39:         }
 40: 
 41:         /// <summary>
 42:         /// plain loop through
 43:         /// moving in chunk of 5: [0][1][2][3][4], [1][2][3][4][5],...,[n-5][n-4][n-3][n-2][n-1]
 44:         /// checking zero doesnt really help in performance(unmeasurable)
 45:         /// i guess the compiler and cpu branch prediction are working at best 🙂
 46:         /// takes around 800ish tick in my machine(core i7 920 @ 2.6ghz, 3gb ddr3)
 47:         /// answer is 40824
 48:         /// ps. the -48 is because the Convert.ToInt32 would return ascii value of the number(so 7 becomes 55)
 49:         /// this is gazillion faster than doing num1 = number[0 + i] - '0', probably because of all the boxing involved
 50:         /// too bad i cant get the profile to work(vs2008 + core i7 + profiler driver = BSOD)
 51:         /// </summary>
 52:         /// <returns></returns>
 53:         static int BruteForce()
 54:         {
 55:             int len = number.Length - 4;
 56:             int curMax = 0;
 57:             int cur = 0;
 58:             int num1 = 0;
 59:             int num2 = 0;
 60:             int num3 = 0;
 61:             int num4 = 0;
 62:             int num5 = 0;
 63: 
 64:             for (int i = 0; i < len; i++)
 65:             {
 66:                 num1 = Convert.ToInt32(number[0 + i]) - 48;
 67:                 num2 = Convert.ToInt32(number[1 + i]) - 48;
 68:                 num3 = Convert.ToInt32(number[2 + i]) - 48;
 69:                 num4 = Convert.ToInt32(number[3 + i]) - 48;
 70:                 num5 = Convert.ToInt32(number[4 + i]) - 48;
 71:                 cur = num1 * num2 * num3 * num4 * num5;
 72:                 if (cur >= curMax)
 73:                 {
 74:                     curMax = cur;
 75:                 }
 76:             }
 77: 
 78:             return curMax;
 79:         }
 80:     }

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