Monday, August 6, 2012

Fun Algorithm Stuff

using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Algorithm.Tests
{
   [TestClass]
   public class UnitTests
   {
      [TestMethod]
      public void PrimeNumberAlgorithmTest()
      {
         int counter = 2;
         List<int> primeNumbers = new List<int>();
         while (counter < 101)
         {
            bool isPrimeNumber = true;
            foreach (int primeNumber in primeNumbers.Where(primeNumber => counter %
                  primeNumber == 0))
            {
               isPrimeNumber = false;
            }
            if (isPrimeNumber)
            {
               primeNumbers.Add(counter);
            }
            counter++;
         }
         Assert.AreEqual(primeNumbers[0], 2);
         Assert.AreEqual(primeNumbers[1], 3);
         Assert.AreEqual(primeNumbers[2], 5);
         Assert.AreEqual(primeNumbers[3], 7);
         Assert.AreEqual(primeNumbers[4], 11);
         Assert.AreEqual(primeNumbers[5], 13);
         Assert.AreEqual(primeNumbers[6], 17);
         Assert.AreEqual(primeNumbers[7], 19);
         Assert.AreEqual(primeNumbers[8], 23);
         Assert.AreEqual(primeNumbers[9], 29);
         Assert.AreEqual(primeNumbers[10], 31);
         Assert.AreEqual(primeNumbers[11], 37);
         Assert.AreEqual(primeNumbers[12], 41);
         Assert.AreEqual(primeNumbers[13], 43);
         Assert.AreEqual(primeNumbers[14], 47);
         Assert.AreEqual(primeNumbers[15], 53);
         Assert.AreEqual(primeNumbers[16], 59);
         Assert.AreEqual(primeNumbers[17], 61);
         Assert.AreEqual(primeNumbers[18], 67);
         Assert.AreEqual(primeNumbers[19], 71);
         Assert.AreEqual(primeNumbers[20], 73);
         Assert.AreEqual(primeNumbers[21], 79);
         Assert.AreEqual(primeNumbers[22], 83);
         Assert.AreEqual(primeNumbers[23], 89);
         Assert.AreEqual(primeNumbers[24], 97);
         Assert.AreEqual(primeNumbers.Count, 25);
      }
      
      [TestMethod]
      public void SumTwoAlgorithmTest()
      {
         int[] array = new int[]{7, 8, 14, 1, -12, 27};
         Assert.AreEqual(IsCapableOfReturningSpecificSumForAnyTwoNumbers(array, 15),
               true);
         array = new int[]{ 7, 8, 14, 1, -12 };
         Assert.AreEqual(IsCapableOfReturningSpecificSumForAnyTwoNumbers(array, 15),
               false);
      }
      
      private bool IsCapableOfReturningSpecificSumForAnyTwoNumbers(int[] array, int
            specificSum)
      {
         bool arrayIsLegitimate = true;
         int outerCounter = 0;
         while (outerCounter < array.Count())
         {
            int innerCounter = 0;
            bool isMatchForCounterOne = false;
            while (innerCounter < array.Count())
            {
               if (outerCounter != innerCounter)
               {
                  if (array[outerCounter] + array[innerCounter] == specificSum)
                  {
                     isMatchForCounterOne = true;
                  }
               }
               innerCounter++;
            }
            if (!isMatchForCounterOne)
            {
               arrayIsLegitimate = false;
            }
            outerCounter++;
         }
         return arrayIsLegitimate;
      }
   }
}

No comments:

Post a Comment