SHARE
TWEET

Euler8

a guest May 18th, 2016 21 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Threading;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             Console.WriteLine("domi       brian3     brian8");
  13.  
  14.             for (int i = 0; i < 1000; i++)
  15.             {
  16.                 ProblemEight.SolutionEight();
  17.                 ProblemEight.SolutionEightAlt3();
  18.                 ProblemEight.SolutionEightAlt8();
  19.                 Thread.Sleep(1000);
  20.             }
  21.         }
  22.     }
  23.  
  24.     class ProblemEight
  25.     {
  26.         static byte[] input = { 7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4, 9, 1, 9, 2, 2, 5, 1, 1, 9, 6, 7, 4, 4, 2, 6, 5, 7, 4, 7, 4, 2, 3, 5, 5, 3, 4, 9, 1, 9, 4, 9, 3, 4, 9, 6, 9, 8, 3, 5, 2, 0, 3, 1, 2, 7, 7, 4, 5, 0, 6, 3, 2, 6, 2, 3, 9, 5, 7, 8, 3, 1, 8, 0, 1, 6, 9, 8, 4, 8, 0, 1, 8, 6, 9, 4, 7, 8, 8, 5, 1, 8, 4, 3, 8, 5, 8, 6, 1, 5, 6, 0, 7, 8, 9, 1, 1, 2, 9, 4, 9, 4, 9, 5, 4, 5, 9, 5, 0, 1, 7, 3, 7, 9, 5, 8, 3, 3, 1, 9, 5, 2, 8, 5, 3, 2, 0, 8, 8, 0, 5, 5, 1, 1, 1, 2, 5, 4, 0, 6, 9, 8, 7, 4, 7, 1, 5, 8, 5, 2, 3, 8, 6, 3, 0, 5, 0, 7, 1, 5, 6, 9, 3, 2, 9, 0, 9, 6, 3, 2, 9, 5, 2, 2, 7, 4, 4, 3, 0, 4, 3, 5, 5, 7, 6, 6, 8, 9, 6, 6, 4, 8, 9, 5, 0, 4, 4, 5, 2, 4, 4, 5, 2, 3, 1, 6,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, 1, 1, 6, 4, 6, 7, 1, 0, 9, 4, 0, 5, 0, 7, 7, 5, 4, 1, 0, 0, 2, 2, 5, 6, 9, 8, 3, 1, 5, 5, 2, 0, 0, 0, 5, 5, 9, 3, 5, 7, 2, 9, 7, 2, 5, 7, 1, 6, 3, 6, 2, 6, 9, 5, 6, 1, 8, 8, 2, 6, 7, 0, 4, 2, 8, 2, 5, 2, 4, 8, 3, 6, 0, 0, 8, 2, 3, 2, 5, 7, 5, 3, 0, 4, 2, 0, 7, 5, 2, 9, 6, 3, 4, 5, 0 };
  27.         static long[] timings = new long[100000];
  28.  
  29.         public static void SolutionEight()
  30.         {
  31.             Stopwatch watch = new Stopwatch();
  32.  
  33.             for (int iterations = 0; iterations < timings.Length; iterations++)
  34.             {
  35.                 long product, max = 0;
  36.  
  37.                 watch.Restart();
  38.  
  39.                 for (short i = 0; i < 987; i++)
  40.                 {
  41.                     product = (long)(input[i] * input[i + 1] * input[i + 2] * input[i + 3] * input[i + 4] * input[i + 5] * input[i + 6]) * (input[i + 7] * input[i + 8] * input[i + 9] * input[i + 10] * input[i + 11] * input[i + 12]);
  42.  
  43.                     max = product > max ? product : max;
  44.                    
  45.                     if (input[i + 12] == 0)
  46.                         i += 13;
  47.                 }
  48.  
  49.                 watch.Stop();
  50.                 timings[iterations] = watch.ElapsedTicks;
  51.             }
  52.  
  53.             Console.Write(timings.Average());
  54.         }
  55.  
  56.         public static void SolutionEightAlt3()
  57.         {
  58.             Stopwatch watch = new Stopwatch();
  59.  
  60.             for (int iterations = 0; iterations < timings.Length; iterations++)
  61.             {
  62.                 watch.Restart();
  63.  
  64.                 long best = 0;
  65.                 uint prevUsable = 0;
  66.  
  67.                 for (uint i = 0; i < 1000; ++i)
  68.                 {
  69.                     if (input[i] == 0)
  70.                     {
  71.                         prevUsable = 0;
  72.                     }
  73.                     else
  74.                     {
  75.                         ++prevUsable;
  76.                         if (prevUsable >= 13)
  77.                         {
  78.                             long prevProduct = (long) (input[i - 12]*
  79.                                                        input[i - 11]*
  80.                                                        input[i - 10]*
  81.                                                        input[i - 9]*
  82.                                                        input[i - 8]*
  83.                                                        input[i - 7])*
  84.                                                (long) (input[i - 6]*
  85.                                                        input[i - 5]*
  86.                                                        input[i - 4]*
  87.                                                        input[i - 3]*
  88.                                                        input[i - 2]*
  89.                                                        input[i - 1]*
  90.                                                        input[i]);
  91.                             best = prevProduct > best ? prevProduct : best;
  92.                         }
  93.                     }
  94.                 }
  95.  
  96.                 watch.Stop();
  97.                 timings[iterations] = watch.ElapsedTicks;
  98.             }
  99.  
  100.             Console.Write(" - " + timings.Average());
  101.         }
  102.  
  103.         public static void SolutionEightAlt8()
  104.         {
  105.             Stopwatch watch = new Stopwatch();
  106.  
  107.             for (int iterations = 0; iterations < timings.Length; iterations++)
  108.             {
  109.                 watch.Restart();
  110.  
  111.                 double best = 0;
  112.                 double prevProduct = 1;
  113.  
  114.                 uint prevUsable = 0;
  115.  
  116.                 for (uint i = 0; i < 1000; ++i)
  117.                 {
  118.                     if (input[i] == 0)
  119.                     {
  120.                         prevUsable = 0;
  121.                         prevProduct = 1;
  122.                     }
  123.                     else
  124.                     {
  125.                         ++prevUsable;
  126.                         prevProduct *= input[i];
  127.                         if (prevUsable > 13)
  128.                         {
  129.                             double recip = 1.0/input[i - 13];
  130.                             prevProduct *= recip;
  131.                         }
  132.                         best = prevProduct > best ? prevProduct : best;
  133.                     }
  134.                 }
  135.  
  136.                 watch.Stop();
  137.                 timings[iterations] = watch.ElapsedTicks;
  138.             }
  139.  
  140.             Console.WriteLine(" - " + timings.Average());
  141.         }
  142.     }
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top