Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BIBLIOTEKA:
- using System;
- using System.Collections;
- namespace Zadanie2
- {
- public class Primes : IEnumerator
- {
- private int licznik;
- private bool PrimeCheck(int n) //sprawdzamy czy jest pierwsza
- {
- if (n < 2) return false;
- for (int i = 2; i < n - 1; i++)
- {
- if (n % i == 0) return false;
- }
- return true;
- }
- public Primes() //konstruktor
- {
- licznik = 1;
- }
- public bool MoveNext() //nastepny element kolekcji, az do max value inta
- {
- licznik++;
- while (!PrimeCheck(licznik)) licznik++;
- return licznik < int.MaxValue;
- }
- public void Reset() //ustawia licznik na 1 (reset)
- {
- licznik = 1;
- }
- public object Current //pobiera biezacy element kolekcji
- {
- get
- {
- return licznik;
- }
- }
- }
- public class PrimeCollection : IEnumerable
- {
- public IEnumerator GetEnumerator()
- {
- return new Primes();
- }
- }
- }
- TESTY:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Zadanie2;
- namespace Zadanie_2_Prime_Testy
- {
- class MainClass
- {
- public static void Main(string[] args)
- {
- Zadanie2.PrimeCollection pc = new Zadanie2.PrimeCollection();
- foreach (int p in pc)
- System.Console.WriteLine(p);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement