Last active
July 3, 2018 23:19
-
-
Save JL-Cox/ad8061afa7f74851687b225cdb9f2e70 to your computer and use it in GitHub Desktop.
Project Euler: 14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace EulerProject_14_06262018 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var dictOfNums = new Dictionary<int, bunchOfNumbers>(); | |
for (int i = 1000000; i > 1; i--) | |
{ | |
bunchOfNumbers newBunch = new bunchOfNumbers(); | |
newBunch.nameOfNumber = i; | |
float currentSeqNum = i; | |
while(currentSeqNum != 1) | |
{ | |
if (currentSeqNum % 2 == 0) | |
{ | |
currentSeqNum = (currentSeqNum/2); | |
newBunch.iterSequence.Add(currentSeqNum); | |
} | |
else if (currentSeqNum % 2 != 0) | |
{ | |
currentSeqNum = ((currentSeqNum * 3) + 1); | |
newBunch.iterSequence.Add(currentSeqNum); | |
} | |
else { break; } | |
} | |
if (currentSeqNum == 1) | |
{ | |
newBunch.lengthOfSeq = newBunch.iterSequence.Count(); | |
dictOfNums.Add(newBunch.nameOfNumber, newBunch); | |
} | |
} | |
float largestSeq = float.MinValue; | |
float nameOfAnswer = 0; | |
foreach (var item in dictOfNums) | |
{ | |
if (item.Value.lengthOfSeq > largestSeq) | |
{ | |
largestSeq = item.Value.lengthOfSeq; | |
nameOfAnswer = item.Value.nameOfNumber; | |
} | |
} | |
Console.WriteLine($"Answer: {nameOfAnswer}; Length: {largestSeq}"); | |
Console.ReadKey(); | |
} | |
} | |
class bunchOfNumbers | |
{ | |
public List<float> iterSequence = new List<float>(); | |
public int nameOfNumber { get; set; } | |
public int lengthOfSeq { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment