Simple demo for Range of Prime Numbers:

static void Main(string[] args)
        {
            List<int> PrimeNumbers = GeneratePrimeNumbers(1, 10);

            foreach (int i in PrimeNumbers)
                System.Diagnostics.Debug.WriteLine(i);
        }

        public static List<int> GeneratePrimeNumbers(int from, int to)
        {
            List<int> numbers = new List<int>();
            int num;
            int i;
            int factor;
            bool isprime;
            if (from == 1) { numbers.Add(from); from++; }
            for (num = from; num < to; num++)
            {
                isprime = true;
                factor = 0;
                // see if num is evenly divisible 
                for (i = 2; i <= num / 2; i++)
                {
                    if ((num % i) == 0)
                    {
                        // num is evenly divisible -- not prime 
                        isprime = false;
                        factor = i;
                        from++;
                    }
                }

                if (isprime)
                  numbers.Add(num); 
            }
            return numbers;
        }
This article has been dead for over six months. Start a new discussion instead.