Want more features on Pastebin? Sign Up, it's FREE!
Guest

Grant Kot

By: a guest on Aug 15th, 2009  |  syntax: C#  |  size: 0.81 KB  |  views: 460  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. static void Problem26() {
  2.         DateTime start = DateTime.Now;
  3.         int numLongestRecurring = 1;
  4.         int cycle = 0;
  5.         for (int i = 1; i < 1000; i++) {
  6.                 int power = 1, lam = 1;
  7.                 int tortoise = 1 / i;
  8.                 int tortoiseRem = (1 % i) * 10;
  9.                 int hare = tortoiseRem / i;
  10.                 int hareRem = (tortoiseRem % i) * 10;
  11.                 while (tortoise != hare || tortoiseRem != hareRem) {
  12.                         if (power == lam) {
  13.                                 tortoise = hare;
  14.                                 tortoiseRem = hareRem;
  15.                                 power *= 2;
  16.                                 lam = 0;
  17.                         }
  18.                         hare = hareRem / i;
  19.                         hareRem = (hareRem % i) * 10;
  20.                         lam++;
  21.                 }
  22.                 if (lam > cycle) {
  23.                         cycle = lam;
  24.                         numLongestRecurring = i;
  25.                 }
  26.         }
  27.         Console.WriteLine("Number with longest recurring cycle: {0}\nCycle Length: {1}", numLongestRecurring, cycle);
  28.         Console.WriteLine((DateTime.Now - start).TotalMilliseconds + " ms");
  29.         Console.ReadLine();
  30. }
clone this paste RAW Paste Data