http://projecteuler.net/problem=42
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …
Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words?
//get a list of 100 triangle numbers LinkedList<double> tri_nums = new LinkedList<double>(); for( int n = 1; n<101 ; n++) { double value = (.5 * n) * (n + 1); tri_nums.AddLast(value); } //evaluate word values and compare StreamReader nameStream; string fullBook = ""; nameStream = File.OpenText("C:\\Euler\\words.txt"); fullBook = nameStream.ReadToEnd(); nameStream.Close(); string[] names = fullBook.Split(','); int tri_count = 0; for (int i = 0; i < names.Length; i++) { int namePosition = (i + 1); string name1 = names[i].Replace('"', ' ').Trim(); int letter_sum = 0; for (int j = 0; j < name1.Length; j++) letter_sum += (int)(name1[j] - 64); if (tri_nums.Contains(letter_sum)) { Console.WriteLine("triangle Word : " + name1 ); tri_count++; } } Console.WriteLine(); Console.WriteLine("Triangle word count : " + tri_count); |