Advertisement
Febrin

Untitled

Mar 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Zad_2
  5. {
  6.     public class Primes : IEnumerator
  7.     {
  8.         private int licznik;
  9.         private int SIZE = 1<<10;
  10.  
  11.         private bool PrimeCheck(int n)  
  12.         {
  13.             if (n < 2) return false;
  14.  
  15.             for (int i = 2; i*i <= n; i++)
  16.             {
  17.                 if (n % i == 0) return false;
  18.             }
  19.  
  20.             return true;
  21.         }
  22.  
  23.         public Primes()
  24.         {
  25.             licznik = 1;
  26.         }
  27.  
  28.         public bool MoveNext()
  29.         {
  30.             licznik++;
  31.             while (!PrimeCheck(licznik)) licznik++;
  32.             return licznik < SIZE;
  33.         }
  34.  
  35.         public void Reset()
  36.         {
  37.             licznik = 1;
  38.         }
  39.  
  40.         public object Current
  41.         {
  42.             get
  43.             {
  44.                 return licznik;
  45.             }
  46.         }
  47.     }
  48.  
  49.  
  50.     class PrimeCollection : IEnumerable
  51.     {
  52.         public IEnumerator GetEnumerator()
  53.         {
  54.             return new Primes();
  55.         }
  56.     }
  57.  
  58.  
  59.     class MainClass
  60.     {
  61.         public static void Main(string[] args)
  62.         {
  63.             PrimeCollection pc = new PrimeCollection();
  64.             foreach (int p in pc)
  65.                 System.Console.WriteLine(p);
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement