Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections;
- namespace lista4zad2
- {
- public class PrimeCollection : IEnumerable
- {
- IEnumerator IEnumerable.GetEnumerator()
- {
- return (IEnumerator) GetEnumerator();
- }
- public PrimeEnum GetEnumerator()
- {
- return new PrimeEnum();
- }
- }
- public class PrimeEnum : IEnumerator
- {
- int prime_num = 1;
- public bool MoveNext()
- {
- for (prime_num = prime_num + 1; prime_num <= Int32.MaxValue; prime_num++)
- {
- bool not_prime = false;
- for (int j = 2; j <= Math.Sqrt(prime_num); j++)
- {
- if (prime_num % j == 0)
- {
- not_prime = true;
- break;
- }
- }
- if (!not_prime)
- {
- break;
- }
- }
- if (prime_num >= Int32.MaxValue) return false;
- return true;
- }
- public void Reset()
- {
- prime_num = 1;
- }
- object IEnumerator.Current
- {
- get
- {
- return Current;
- }
- }
- public int Current
- {
- get
- {
- return prime_num;
- }
- }
- }
- public class lista4zad2
- {
- static void Main()
- {
- PrimeCollection pc = new PrimeCollection();
- foreach(int p in pc)
- Console.WriteLine(p);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement