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/exercise02.cpp | 18 +++++++++++++++ Sem. 04/Solutions/exercise03.cpp | 26 +++++++++++++++++++++ Sem. 04/Solutions/exercise04.cpp | 50 ++++++++++++++++++++++++++++++++++++++++ Sem. 04/Solutions/exercise05.cpp | 33 ++++++++++++++++++++++++++ Sem. 04/Solutions/exercise06.cpp | 38 ++++++++++++++++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 Sem. 04/Solutions/exercise02.cpp create mode 100644 Sem. 04/Solutions/exercise03.cpp create mode 100644 Sem. 04/Solutions/exercise04.cpp create mode 100644 Sem. 04/Solutions/exercise05.cpp create mode 100644 Sem. 04/Solutions/exercise06.cpp 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 +#include + +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 +#include + +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 +#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); +} 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 +#include + +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 +#include + +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); +} -- cgit v1.3.1-23-gd82d From ab5d7fd63e23ca472b01058a3df0353b027458af Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 7 Nov 2021 16:25:55 +0200 Subject: Removed unnecessary abs function calls --- Sem. 04/Solutions/exercise02.cpp | 2 +- Sem. 04/Solutions/exercise03.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sem. 04/Solutions/exercise02.cpp b/Sem. 04/Solutions/exercise02.cpp index 700962e..b7ad887 100644 --- a/Sem. 04/Solutions/exercise02.cpp +++ b/Sem. 04/Solutions/exercise02.cpp @@ -5,7 +5,7 @@ using namespace std; double distanceBetweenPoints(int x1, int y1, int x2, int y2) { - int sideA = abs(x1 - x2), sideB = abs(y1 - y2); + int sideA = x1 - x2, sideB = y1 - y2; return sqrt(sideA * sideA + sideB * sideB); // Pythagorean theorem } diff --git a/Sem. 04/Solutions/exercise03.cpp b/Sem. 04/Solutions/exercise03.cpp index c238d44..63c522b 100644 --- a/Sem. 04/Solutions/exercise03.cpp +++ b/Sem. 04/Solutions/exercise03.cpp @@ -6,7 +6,7 @@ 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); + int sideA = x1 - x2, sideB = y1 - y2; return sqrt(sideA * sideA + sideB * sideB); // Pythagorean theorem } -- cgit v1.3.1-23-gd82d