Lucky Numbers Sample C# Code

Code:

    public class LuckyNumbers
    {
        public string GetLuckyNumbers(int range)
        {
            return PrintResult(FindLuckyNumbers(range));
        }

        public string GetLuckyPrimeNumbers(int range)
        {
            bool[] numbers = FindLuckyNumbers(range);

            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i]) continue;
                numbers[i] = !IsPrimeNumber(i+1);
            }

            return PrintResult(numbers);
        }

        private bool[] FindLuckyNumbers(int range)
        {
            if (range < 1) range = 0;
            bool[] numbers = new bool[range];
            int luckyCounter = 2;

            while (luckyCounter < numbers.Length)
            {
                luckyCounter = StrikeOutNumbers(numbers, luckyCounter);
            }

            return numbers;
        }

        private int StrikeOutNumbers(bool[] numbers, int luckyCounter)
        {
            int strikeCounter = 0;

            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i]) continue;
                strikeCounter++;
                
                if (strikeCounter == luckyCounter)
                {
                    numbers[i] = true;
                    strikeCounter = 0;
                }
            }

            return GetLuckyCounter(numbers, luckyCounter);
        }

        private int GetLuckyCounter(bool[] numbers, int skip)
        {
            if (skip >= numbers.Length) return numbers.Length;

            for (int i = skip; i < numbers.Length; i++)
            {
                if (!numbers[i]) return i+1;
            }

            return numbers.Length;
        }

        private static bool IsPrimeNumber(int number)
        {
            if (number == 1) return false;

            for (short i = 3; i <= Math.Sqrt(number); i += 2)
            {
                if (number % i == 0) return false;
            }

            return true;
        }

        private static string PrintResult(bool[] numbers)
        {
            string result = string.Empty;

            for (int i = 0; i < numbers.Length; i++)
            {
                if (!numbers[i]) result = result + (i+1) + ",";
            }

            if (!string.IsNullOrEmpty(result)) result = result.Substring(0, result.Length - 1);

            return result;
        }
    }

Test:

    [TestClass]
    public class LuckyNumbersTest
    {
        [TestMethod]
        public void GetLuckyNumbersTest()
        {
            // arrange
            LuckyNumbers lucky = new LuckyNumbers();
            IDictionary<int, string> testCases = new Dictionary<int, string>();
            testCases.Add(-1, "");
            testCases.Add(0, "");
            testCases.Add(1, "1");
            testCases.Add(10, "1,3,7,9");
            testCases.Add(30, "1,3,7,9,13,15,21,25");
            testCases.Add(100, "1,3,7,9,13,15,21,25,31,33,37,43,49,51,63,67,69,73,75,79,87,93,99");

            // act
            foreach (var testCase in testCases)
            {
                string result = lucky.GetLuckyNumbers(testCase.Key);

                // assert
                Assert.AreEqual(testCase.Value, result);
            }
        }

        [TestMethod]
        public void GetLuckyPrimeNumbersTest()
        {
            // arrange
            LuckyNumbers lucky = new LuckyNumbers();
            IDictionary<int, string> testCases = new Dictionary<int, string>();
            testCases.Add(-1, "");
            testCases.Add(0, "");
            testCases.Add(1, "");
            testCases.Add(10, "3,7");
            testCases.Add(30, "3,7,13");
            testCases.Add(100, "3,7,13,31,37,43,67,73,79");

            // act
            foreach (var testCase in testCases)
            {
                string result = lucky.GetLuckyPrimeNumbers(testCase.Key);

                // assert
                Assert.AreEqual(testCase.Value, result);
            }
        }
    }