Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitb5326685d85e2db647769cf3c1ac10e2ee18567d (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2021-11-18 11:25:56 +0200
Committer
 GitHub <noreply@github.com>
2021-11-18 11:25:56 +0200
Files4e4f08cf2990246b17e75d0eadeecd90c5bd1621
Introduction_to_programming_FMI-b5326685d85e2db647769cf3c1ac10e2ee18567d.tar
Introduction_to_programming_FMI-b5326685d85e2db647769cf3c1ac10e2ee18567d.tar.gz
Introduction_to_programming_FMI-b5326685d85e2db647769cf3c1ac10e2ee18567d.zip
Parent4fb338972b11cdfc856530406f0999523952bf79 (diff)
Parentab5d7fd63e23ca472b01058a3df0353b027458af (diff)
Merge pull request #14 from Syndamia/sem04-solutions
Solved ALL unsolved exercises from "Sem. 04"
5 files changed, 165 insertions, 0 deletions
-rw-r--r--Sem. 04/Solutions/exercise02.cpp18
-rw-r--r--Sem. 04/Solutions/exercise03.cpp26
-rw-r--r--Sem. 04/Solutions/exercise04.cpp50
-rw-r--r--Sem. 04/Solutions/exercise05.cpp33
-rw-r--r--Sem. 04/Solutions/exercise06.cpp38
diff --git a/Sem. 04/Solutions/exercise02.cpp b/Sem. 04/Solutions/exercise02.cpp
new file mode 100644
index 0000000..b7ad887
--- /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 = x1 - x2, sideB = 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..63c522b
--- /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 = x1 - x2, sideB = 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);
+}