Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- #region Primes class
- public class Primes : IEnumerable<uint>
- {
- private uint val = 1;
- public IEnumerator<uint> GetEnumerator()
- {
- bool isPrime;
- uint maxDivider;
- while (true)
- {
- isPrime = true;
- maxDivider = (uint)Math.Sqrt(val);
- for (int i = 2; i <= maxDivider; i++)
- {
- if (val % i == 0)
- {
- isPrime = false;
- break;
- }
- }
- val++;
- if (isPrime)
- yield return val - 1;
- }
- }
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- #endregion
- class ForEachExample
- {
- static void Main()
- {
- //// An array of integers
- //int[] numbers = { 10, 32, 3, 54, 15 };
- //foreach (int n in numbers)
- //{
- // Console.WriteLine(n);
- //}
- //// An array of strings
- //string[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
- //foreach (string day in days)
- //{
- // Console.WriteLine(day);
- //}
- //endless loop
- Primes primes = new Primes();
- foreach (uint prime in primes)
- {
- Console.WriteLine(prime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment