How to find out prime numbers using Iterators in C#

Iterators : Iterator is a block of code that supplies all the values to be used in a foreach block in sequence. Typically, this block of code will be a method, although it is also possible to use property accessors and other blocks of code as iterators.

  • IEnumerable interface is responsible for allowing you to use foreach loops
  • If you want to iterate over a class, use a method called GetEnumerator() with a return type of IEnumerator.
  • If you want to iterate over a class member, such as a method, use IEnumerable
  • Within an iterator block you select the values to be used in the foreach loop using the yield keyword

Implementing an Iterator:

// Program: Iterations
// Purpose: Training period
// By: H.M. Saiful Islam

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Iterators
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string item in SimpleTest())
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

            Primes PFT = new Primes(2,100);
            foreach (long i in PFT)
            {
                Console.WriteLine("{0}", i);
            }
            Console.ReadKey();
        }

        // simple example for understanding iterators
        public static IEnumerable SimpleTest()
        {
            yield return "palash";
            yield return "forhad";
            yield return "gisaan";
            yield return "ridoy";
        }

        // implementing an Iterator
        public class Primes
        {
            private long min;
            private long max;

            public Primes()
                : this(2, 100)
            {}

            public Primes(long minimum, long maximum)
            {
                if (min < 2)
                {
                    min = 2;
                }
                min = minimum;
                max = maximum;
            }

            // find out prime numbers using Iterator
            public IEnumerator GetEnumerator()
            {
                for (long PP = min; PP <= max; PP++)
                {
                    bool isPrime = true;
                    for (long PF = 2; PF <= (long)Math.Floor(Math.Sqrt(PP)); PF++)
                    {
                        long RAD = PP % PF;
                        if (RAD == 0)
                        {
                            isPrime = false;
                            break;
                        }
                    }
                    if (isPrime)
                    {
                        yield return PP;
                    }

                }
            }
        }

    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s