Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 04 / Solutions / exercise04.cpp
blob: 56909ba09e01615c9079f632d7be0e49e746ab4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cmath>

using namespace std;

bool isPrime(int n)
{
	double rootOfN = sqrt(n);
	for (int i = 2; i <= rootOfN; i++)
	{
		if (n % i == 0)
			return false;
	}
	return true;
}

int countOfUniqueSimpleDivisors(int n)
{
	int divisorsCount = 0;
	for (int i = 2; i <= n && n != 1; i++)
	{
		if (n % i == 0 && isPrime(i))
		{
			n /= i;
			divisorsCount++;

			if (n == 1)
				return divisorsCount;
		}
	}
	return -1; // Some divisors repeat themselves, so we can't have a proper count
}

void kCountOfSimpleDivisorsBetween(int a, int b, int k)
{
	for (int i = a; i <= b; i++)
	{
		if (countOfUniqueSimpleDivisors(i) == k)
			cout << i << " ";
	}
	cout << endl;
}

int main()
{
	int a, b, k;
	cin >> a >> b >> k;

	kCountOfSimpleDivisorsBetween(a, b, k);
}