| Changes to /trunk/Csharp/euler14.cs |
r0 vs. r6
Edit
|
r6
|
| /trunk/Csharp/euler14.cs | /trunk/Csharp/euler14.cs r6 | ||
| 1 | static long CountTerms(long x, ref long[] terms) | ||
|---|---|---|---|
| 2 | { | ||
| 3 | long tc = 1; | ||
| 4 | long k = x; | ||
| 5 | |||
| 6 | while (k!=1) | ||
| 7 | { | ||
| 8 | tc++; | ||
| 9 | k = (k % 2 == 0) ? k / 2 : 3 * k + 1; | ||
| 10 | if (k <= x) | ||
| 11 | { | ||
| 12 | if (terms[k] > 0) | ||
| 13 | { | ||
| 14 | terms[x] = terms[k] + tc; | ||
| 15 | return terms[x]; | ||
| 16 | } | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | terms[x] = tc; | ||
| 21 | |||
| 22 | return terms[x] ; | ||
| 23 | } | ||
| 24 | |||
| 25 | static long LongestSeq() | ||
| 26 | { | ||
| 27 | long max = 1000000; | ||
| 28 | long[] termcount = new long[max+1]; | ||
| 29 | long tcmax = 0; | ||
| 30 | long c = 0; | ||
| 31 | long ix = 0; | ||
| 32 | |||
| 33 | for (int i = 1; i <= max; i++) | ||
| 34 | { | ||
| 35 | c = CountTerms(i, ref termcount); | ||
| 36 | if ( c > tcmax) | ||
| 37 | { | ||
| 38 | tcmax = c; | ||
| 39 | ix = i; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | return ix; | ||
| 44 | } | ||
| 45 | |||
| 46 | static void Main(string[] args) | ||
| 47 | { | ||
| 48 | Console.WriteLine(LongestSeq()); | ||
| 49 | Console.ReadLine(); | ||
| 50 | } | ||