Skip to content

Commit

Permalink
Problem 14 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberpunk2099 committed Jul 2, 2019
1 parent b54402a commit 91f60e6
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/ProjectEuler/ProjectEuler.Problems/Solutions/Problem014.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectEuler.Problems.Solutions
{
Expand All @@ -23,9 +20,38 @@ public class Problem014 : IProblem
"Which starting number, under one million, produces the longest chain? \r\n\r\n" +
"NOTE: Once the chain starts the terms are allowed to go above one million.";

// TODO: Optimization?
public string GetSolution()
{
throw new NotImplementedException();
var num = 0;
var length = 0;
for (int i = 999_999; i > 1; i--)
{
var len = GetCollatzSequence(i).Count();
if (len > length)
{
length = len;
num = i;
}
}
return $"{num} ({length} terms)";
}

private static IEnumerable<int> GetCollatzSequence(int i)
{
yield return i;
while (i > 1)
{
yield return i = GetNext(i);
}
}

private static int GetNext(int i)
{
if (i % 2 == 0)
return i / 2;
else
return (3 * i) + 1;
}
}
}

0 comments on commit 91f60e6

Please sign in to comment.