Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class ListaLeniwa
- {
- ListaLeniwa nast;
- protected int ile;
- protected int wartosc;
- protected static Random losowa = new Random();
- public ListaLeniwa()
- {
- wartosc = 0;
- ile = 0;
- this.nast = null;
- }
- virtual public int elementy(int i)
- {
- if (i <= 0)
- {
- System.Console.WriteLine("Bledna liczba\n");
- return 0;
- }
- if (ile == 0)
- {
- ListaLeniwa pom;
- this.wartosc = losowa.Next();
- this.nast = new ListaLeniwa();
- pom = this.nast;
- for (int x = 0; x < i; x++)
- {
- pom.wartosc = losowa.Next();
- pom.nast = new ListaLeniwa();
- pom = pom.nast;
- this.ile++;
- }
- return 0;
- }
- if (i < ile)
- {
- ListaLeniwa pom = this;
- for (int x = 0; x < i; x++)
- {
- pom = pom.nast;
- }
- return pom.wartosc;
- }
- if (i > ile)
- {
- ListaLeniwa pom = this;
- for (int x = 0; x < ile; x++, i--)
- {
- pom = pom.nast;
- }
- for (int x = 0; x < i; x++)
- {
- pom.nast = new ListaLeniwa();
- pom.wartosc = losowa.Next();
- ile++;
- pom = pom.nast;
- }
- return 0;
- }
- return 0;
- }
- }
- class Pierwsze : ListaLeniwa
- {
- int z;
- int[] liczby;
- Pierwsze nast;
- public Pierwsze()
- {
- z = 0;
- int n = 1000;
- bool[] tab = new bool[n+1];
- for (int i = 2; i * i <= n; i++)
- {
- if (tab[i] == true)
- {
- continue;
- }
- for (int j = 2 * i; j <= n; j += i)
- {
- tab[j] = true;
- }
- }
- for (int i = 2; i <= n; i++)
- {
- if (tab[i] == false)
- {
- z++;
- }
- }
- liczby = new int[z];
- int w = 0;
- for (int i = 2; i <= n; i++)
- {
- if (tab[i] == false)
- {
- liczby[w] = i;
- w++;
- }
- }
- }
- override public int elementy(int i)
- {
- if (i <= 0)
- {
- System.Console.WriteLine("Bledna liczba\n");
- return 0;
- }
- if (ile == 0)
- {
- Pierwsze pom;
- this.wartosc = this.liczby[losowa.Next(this.z)];
- this.nast = new Pierwsze();
- pom = this.nast;
- for (int x = 0; x < i; x++)
- {
- pom.wartosc = this.liczby[losowa.Next(this.z)];
- pom.nast = new Pierwsze();
- pom = pom.nast;
- this.ile++;
- }
- return 0;
- }
- if (i < ile)
- {
- Pierwsze pom = this;
- for (int x = 0; x < i; x++)
- {
- pom = pom.nast;
- }
- return this.liczby[losowa.Next(this.z)];
- }
- if (i > ile)
- {
- Pierwsze pom = this;
- for (int x = 0; x < ile; x++, i--)
- {
- pom = pom.nast;
- }
- for (int x = 0; x < i; x++)
- {
- pom.nast = new Pierwsze();
- pom.wartosc = this.liczby[losowa.Next(this.z)];
- ile++;
- pom = pom.nast;
- }
- return 0;
- }
- return 0;
- }
- }
- class Program
- {
- static void Main()
- {
- Pierwsze moje;
- moje = new Pierwsze();
- moje.elementy(100);
- for (int x = 1; x < 100; x++)
- System.Console.WriteLine("{0} Liczba {1}\n", x, moje.elementy(x));
- moje.elementy(102);
- for (int x = 1; x < 103; x++)
- System.Console.WriteLine("{0} Liczba {1}\n", x, moje.elementy(x));
- }
- }
- }
Add Comment
Please, Sign In to add comment