Exemplo n.º 1
0
        private static bool IsTriangleNumber(int s)
        {
            if (triangleNums.Count() == 0 || triangleNums.Max() < s)
            {
                triangleNums = TriangleNumberGenerator.Generate().TakeWhile(n => n <= s).ToList();
            }

            return(triangleNums.Contains(s));
        }
        public void TestTriangleNumberGenerator()
        {
            var triangeNumbers = TriangleNumberGenerator.Generate().Take(7).ToArray();
            var expected       = new[] { 1, 3, 6, 10, 15, 21, 28 };

            Assert.AreEqual(expected.Length, triangeNumbers.Length);

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], (int)triangeNumbers[i]);
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            // for this problem, we don't need to know the actual divisors, only the number of them
            // if you factor a number in to its prime factors, you can calculate the number of divisors
            var triangleNumbers = TriangleNumberGenerator.Generate()
                                  .Select(l =>
            {
                var numberOfFactors = long.Parse(l.ToString()).Factorize().Count();
                Console.WriteLine(String.Format("{0} {1}", l, numberOfFactors));

                return(new
                {
                    l = l,
                    numFactors = numberOfFactors
                });
            })
                                  .Where(l => l.numFactors > 500)
                                  .FirstOrDefault();

            Console.WriteLine(triangleNumbers);
            Console.ReadKey();
        }