-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEulerFourteen.cs
51 lines (48 loc) · 1.3 KB
/
EulerFourteen.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProjectEulerFourteen
{
class EulerFourteen
{
static long num = 13, numTerms = 0;
static long temp;
static Dictionary<long, long> solution = new Dictionary<long, long>();
public static void processNumber()
{
numTerms = 0;
while (num != 1)
{
if (num % 2 == 0)
{
num /= 2;
numTerms++;
}
else
{
num = (3 * num) + 1;
numTerms++;
}
}
solution.Add(temp, numTerms);
}
public static void Main()
{
for (long i = 2; i < 1000000; i++)
{
num = i;
temp = num;
processNumber();
}
Console.Write("Max value: ");
Console.WriteLine(solution.Values.Max());
// I LOVE LINQ
var key = solution.Where(p => p.Value == 524).Select(p => p.Key);
Console.Write("Corresponding key:");
foreach (var k in key)
Console.WriteLine(k);
Console.Read();
}
}
}