diff options
Solved all unsolved exercises from Seminar 4
5 files changed, 165 insertions, 0 deletions
| -rw-r--r-- | Sem. 04/Solutions/exercise02.cpp | 18 | ||||
| -rw-r--r-- | Sem. 04/Solutions/exercise03.cpp | 26 | ||||
| -rw-r--r-- | Sem. 04/Solutions/exercise04.cpp | 50 | ||||
| -rw-r--r-- | Sem. 04/Solutions/exercise05.cpp | 33 | ||||
| -rw-r--r-- | Sem. 04/Solutions/exercise06.cpp | 38 |
diff --git a/Sem. 04/Solutions/exercise02.cpp b/Sem. 04/Solutions/exercise02.cpp new file mode 100644 index 0000000..700962e --- /dev/null +++ b/Sem. 04/Solutions/exercise02.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <cmath> + +using namespace std; + +double distanceBetweenPoints(int x1, int y1, int x2, int y2) +{ + int sideA = abs(x1 - x2), sideB = abs(y1 - y2); + return sqrt(sideA * sideA + sideB * sideB); // Pythagorean theorem +} + +int main() +{ + int x1, y1, x2, y2; + cin >> x1 >> y1 >> x2 >> y2; + + cout << distanceBetweenPoints(x1, y1, x2, y2) << endl; +} diff --git a/Sem. 04/Solutions/exercise03.cpp b/Sem. 04/Solutions/exercise03.cpp new file mode 100644 index 0000000..c238d44 --- /dev/null +++ b/Sem. 04/Solutions/exercise03.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <cmath> + +using namespace std; + +// Taken from exercise02.cpp +double distanceBetweenPoints(int x1, int y1, int x2, int y2) +{ + int sideA = abs(x1 - x2), sideB = abs(y1 - y2); + return sqrt(sideA * sideA + sideB * sideB); // Pythagorean theorem +} + +double trianglePerimeterFromPoints(int x1, int y1, int x2, int y2, int x3, int y3) +{ + return distanceBetweenPoints(x1, y1, x2, y2) + + distanceBetweenPoints(x1, y1, x3, y3) + + distanceBetweenPoints(x2, y2, x3, y3); +} + +int main() +{ + int x1, y1, x2, y2, x3, y3; + cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; + + cout << trianglePerimeterFromPoints(x1, y1, x2, y2, x3, y3) << endl; +} 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 <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); +} diff --git a/Sem. 04/Solutions/exercise05.cpp b/Sem. 04/Solutions/exercise05.cpp new file mode 100644 index 0000000..cfe60e4 --- /dev/null +++ b/Sem. 04/Solutions/exercise05.cpp @@ -0,0 +1,33 @@ +#include <iostream> +#include <cmath> + +using namespace std; + +// Taken from exercise04.cpp +bool isPrime(int n) +{ + double rootOfN = sqrt(n); + for (int i = 2; i <= rootOfN; i++) + { + if (n % i == 0) + return false; + } + return true; +} + +void printPrimeNumberPairs(int n) +{ + for (int i = 2; i <= n / 2; i++) + { + if (isPrime(i) && isPrime(n - i)) + cout << i << ", " << n - i << endl; + } +} + +int main() +{ + int n; + cin >> n; + + printPrimeNumberPairs(n); +} diff --git a/Sem. 04/Solutions/exercise06.cpp b/Sem. 04/Solutions/exercise06.cpp new file mode 100644 index 0000000..3ced4c3 --- /dev/null +++ b/Sem. 04/Solutions/exercise06.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <cmath> + +using namespace std; + +bool isPowerOfSomething(int n) +{ + double rootOfN = sqrt(n); + for (int i = 2; i <= rootOfN; i++) + { + if (n % i == 0) + { + while (n % i == 0) + n /= i; + + return n == 1; + } + } + return false; // Obligatory default case +} + +void powerNumbersBetween(int a, int b) +{ + for (int i = a; i <= b; i++) + { + if (isPowerOfSomething(i)) + cout << i << " "; + } + cout << endl; +} + +int main() +{ + int a, b; + cin >> a >> b; + + powerNumbersBetween(a, b); +} |
