Using System. Collections;
Public class Primes // Primes Prime Number
{
# Region Property
Private long min;
Private long max;
# Endregion
# Region Constructor
Public Primes (): this (2,100)
{}
Public Primes (long minimum, long maximum)
{
If (min <2)
Min = 2;
Else
Max = maximum;
}
# Endregion
# Region Method
Public IEnumerator GetEnumerator ()
{
For (long possiblePrime = min; possiblePrime <= max; possiblePrime ++)
{
Bool isPrime = true; // The default value is a prime number, which is expressed as true.
For (long possibleFactor = 2; possibleFactor <= (long) Math. Floor (Math. Sqrt (possiblePrime); possibleFactor ++)
{
Long remainderAfterDivision = possiblePrime % possibleFactor;
If (remainderAfterDivision = 0)
{
IsPrime = false; // indicates the sum of values.
Break;
}
}
If (isPrime)
{
Yield return possiblePrime;
}
}
}
# Endregion
}
Public class Program
{
Static void Main (string [] args)
{
Primes primesFrom2To1000 = new Primes (2, 1000 );
Foreach (long I in primesFrom2To1000)
Console. Write ("{0}", I );
Console. ReadKey ();
}
}
Output: displays a result at a time, with a pause in the middle, instead of displaying all results at a time.
No matter whether the code is terminated before yieid is called, The iterator code returns a result at a time.
How do I display all the prime numbers in the range at a time ???