# C#: Project Euler Solutions to Problems 14, 16, and 22

More ProjectEuler.net fun.

Problem 14

The following iterative sequence is defined for the set of positive integers:

n n/2 (n is even)
n 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 – 40 – 20 – 10 – 5 – 16 – 8 – 4 – 2 – 1

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.

Which starting number, under one million, produces the longest chain?

```static void Main(string[] args) { //populate dictionary with steps and number Dictionary dictionary = new Dictionary(); for (int i = 1; i &lt; 1000000; i++) { Int64 mySteps = getSteps(i); if ( !dictionary.ContainsKey(mySteps) ) { dictionary.Add(mySteps,i); } } //sort the dictionary from least amount of steps to most var list = dictionary.Keys.ToList(); list.Sort(); foreach (var key in list) { Console.WriteLine("Number : {1} \t Steps : {0}", key, dictionary[key]); } }   public static Int64 getSteps(Int64 i) { Int64 startInt = i; Int64 steps = 0; while (true) { if (startInt == 1) break; steps++; if (startInt % 2 == 0) startInt = startInt / 2; else startInt = (startInt * 3) + 1; } return steps; }```

Problem 16

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 21000?

```//2^1000 string string two_pow_1000 = @"10 715 086 071 862 673 209 484 250 490 600 018 105 614 048 117 055 336 074 437 503 883 703 510 511 249 361 224 931 983 788 156 958 581 275 946 729 175 531 468 251 871 452 856 923 140 435 984 577 574 698 574 803 934 567 774 824 230 985 421 074 605 062 371 141 877 954 182 153 046 474 983 581 941 267 398 767 559 165 543 946 077 062 914 571 196 477 686 542 167 660 429 831 652 624 386 837 205 668 069 376"; int sum = 0; for (int i = 0; i &lt; two_pow_1000.Length; i++) { if (two_pow_1000[i] != ' ' ) sum += Int32.Parse(two_pow_1000[i].ToString()); } Console.WriteLine(sum);```

Problem 22

Using names.txt (right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 X 53 = 49714.

What is the total of all the name scores in the file?

```StreamReader nameStream; string fullBook = ""; nameStream = File.OpenText("C:\\Euler\\names.txt"); fullBook = nameStream.ReadToEnd(); nameStream.Close(); string[] names = fullBook.Split(','); Array.Sort(names); int totalSum = 0; for (int i = 0; i &lt; names.Length; i++) { int namePosition = (i + 1); string name = names[i].Replace('"', ' ').Trim(); int lettersum = 0; for (int j = 0; j &lt; name.Length; j++) { lettersum += (int)(name[j] - 64); } totalSum += (lettersum * (i + 1)); } Console.WriteLine("Answer : "+ totalSum);```