-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP14.cs
50 lines (43 loc) · 1.25 KB
/
P14.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
using System;
namespace P1 {
class Program
{
public static int GetCollatzSequenceLength(long num)
{
int count = 1;
while (num > 1) // won't work for 1 itself, when 1 is the starting number
{
if (num % 2 == 0)
{
num = num / 2;
}
else
{
num = (3 * num) + 1;
}
count++;
}
return count;
}
public static int GetLongestCollatzSequence(int limit)
{
int longestSequence = 0;
int startNumber = 0;
for (int i = 2; i <= limit; i++)
{
int sequenceLength = GetCollatzSequenceLength(i);
if (sequenceLength > longestSequence)
{
longestSequence = sequenceLength;
startNumber = i;
}
}
return startNumber;
}
public static void Main(string[] args)
{
int limit = 1000000;
Console.WriteLine("{0:n0}", GetLongestCollatzSequence(limit));
}
}
}