-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem014.cs
95 lines (83 loc) · 2.36 KB
/
Problem014.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
namespace ProjectEuler.Solutions
{
using System.Collections.Generic;
using NUnit.Framework;
/// <summary>
/// Longest Collatz sequence.
/// The following iterative sequence is defined for the set of positive integers:
/// <para>n → n/2 (n is even)</para>
/// <para>n → 3n + 1 (n is odd)</para>
/// Using the rule above and starting with 13, we generate the following sequence:
/// <para>13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1</para>
/// It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
/// <para>Which starting number, under one million, produces the longest chain?</para>
/// <b>NOTE:</b> Once the chain starts the terms are allowed to go above one million.
/// </summary>
public class Problem014 : Problem
{
public override long Solution()
{
IDictionary<long, int> alreadyProcessed = new Dictionary<long, int>();
int startingNumber = 0;
int longestChain = 0;
for(int i = 2; i < 1000000; i++)
{
if(!alreadyProcessed.ContainsKey(i))
{
int chainLength = GetCollatzLength(i, alreadyProcessed);
if(chainLength > longestChain)
{
longestChain = chainLength;
startingNumber = i;
}
}
}
return startingNumber;
}
private static int GetCollatzLength(int start, IDictionary<long, int> alreadyProcessed)
{
int offset = 0;
List<long> sequence = new List<long>
{
start
};
long number = start;
while (number > 1)
{
if (number % 2 == 0)
{
number = number / 2;
}
else
{
number = (number * 3) + 1;
}
if(alreadyProcessed.ContainsKey(number))
{
offset = alreadyProcessed[number];
break;
}
sequence.Add(number);
}
int length = sequence.Count;
for(int i = 0; i < length; i++)
{
long value = sequence[i];
alreadyProcessed.Add(value, length - i + offset);
}
return length + offset;
}
[Test]
public void TestForExample()
{
int[] expectedSequence = new int[] { 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 };
long collatzLength = GetCollatzLength(13, new Dictionary<long, int>());
Assert.AreEqual(expectedSequence.Length, collatzLength);
}
[Test]
public void TestForProblem()
{
Assert.AreEqual(837799, this.Solution());
}
}
}