#include <iostream>

struct elem
{
	int val;
	bool prime;
	elem * next;
	elem(int v, bool p, elem * n) : val(v), next(n), prime(p) {}
};

class Kolechko
{
public:
	Kolechko(int size)
	{
		first = new elem(2, true, NULL);
		first->next = first;
		elem * prev = first;
		elem * t;
		for (int i = 0; i < size - 1; i++)
		{
			t = new elem(prev->val + 1, true, first);
			prev->next = t;
			prev = t;
		}
	}

	void show()
	{
		elem * t = first;
		while(t->next != first)
		{
			if (t->prime)
			{
				std::cout << t->val << std::endl;
			}
			t = t->next;
		}
	}

	void calc(int step = 2)
	{
		int count = step - 1;
		elem * t = first;
		for (int i = 0; i < count; i++)
		{
			t = t->next;
		}
		elem * s = t;
		while(t->next != first)
		{
			if (count < 1)
			{
				count = step - 1;
				t->prime = false;
				t = t->next;
			}
			else
			{
				t = t->next;
				count--;
			}
		}
		if (s->next != first)
			calc(s->val);
	}

private:
	elem * first;
};

int main()
{
	Kolechko * kolco = new Kolechko(100);
	kolco->calc();
	kolco->show();
	return 0;
}