From ab31cac5ab5509785221d7420f54993b51fa8a7f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 3 Nov 2021 20:42:13 +0200 Subject: Solved all unsolved exercises from Seminar 4 --- Sem. 04/Solutions/exercise04.cpp | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Sem. 04/Solutions/exercise04.cpp (limited to 'Sem. 04/Solutions/exercise04.cpp') diff --git a/Sem. 04/Solutions/exercise04.cpp b/Sem. 04/Solutions/exercise04.cpp new file mode 100644 index 0000000..56909ba --- /dev/null +++ b/Sem. 04/Solutions/exercise04.cpp @@ -0,0 +1,50 @@ +#include +#include + +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); +} -- cgit v1.3.1-23-gd82d