Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2.  
  3. namespace Euler118
  4. {
  5.     class Program
  6.     {
  7.         //there are zero bouncy upto 100
  8.         static long seed = 100;
  9.         static long digits = 1000;
  10.         static int i = 1;
  11.         static long ?auxNumber = null;
  12.         static bool decreasing, increasing ;
  13.         static int bounceCount = 0;
  14.         static long bouncyNumbers = 0;
  15.         static double per = 0;
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  20.             Console.WriteLine("Please enter the percentage of Bouncy numbers to be found");
  21.             double perLimit = Convert.ToDouble(Console.ReadLine());
  22.  
  23.             sw.Start();
  24.             while (per < perLimit)
  25.             {
  26.                     var tmpNum = ++seed;
  27.                     if (digits < seed) { digits *= 10; }
  28.                     do
  29.                     {
  30.                         i *= 10;
  31.                         var res = tmpNum % 10;
  32.                         tmpNum = (tmpNum - res) / 10;
  33.  
  34.                         if (auxNumber < res) { increasing = true; }
  35.                         else if (auxNumber > res) { decreasing = true; }
  36.  
  37.                         if (!(increasing ^ decreasing) &&
  38.                             (auxNumber != null) && (auxNumber != res)){
  39.                             bounceCount += 1;
  40.                         }
  41.                         if (bounceCount > 0) { bouncyNumbers++; break; }
  42.                         auxNumber = res;
  43.  
  44.                     } while (i < digits);
  45.  
  46.                     increasing = decreasing = false;
  47.                     bounceCount = 0;
  48.                     auxNumber = null;
  49.                     i = 1;
  50.                     double count = bouncyNumbers;
  51.                     per = (count / seed) * 100;
  52.             }
  53.             sw.Stop();
  54.  
  55.             Console.WriteLine("Percentage Achieved : "+per+"%");
  56.             Console.WriteLine("Total numbers generated : " + seed);
  57.             Console.WriteLine("Bouncy Numbers found : " + bouncyNumbers);
  58.             Console.WriteLine("Time Elapsed : "+sw.ElapsedMilliseconds +" ms");
  59.             Console.ReadLine();
  60.         }
  61.  
  62.     }
  63. }