diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/UNI_Python.iml b/.idea/UNI_Python.iml new file mode 100644 index 0000000..71bf193 --- /dev/null +++ b/.idea/UNI_Python.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..263cea9 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,25 @@ + + + + \ No newline at end of file diff --git a/.idea/jupyter-settings.xml b/.idea/jupyter-settings.xml deleted file mode 100644 index fa111dc..0000000 --- a/.idea/jupyter-settings.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 7489572..c20271c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,8 +1,4 @@ -<<<<<<< Updated upstream - -======= - ->>>>>>> Stashed changes + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 614b3c1..27d55c0 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/python.iml b/.idea/python.iml deleted file mode 100644 index 64b45c5..0000000 --- a/.idea/python.iml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - -<<<<<<< Updated upstream - -======= - ->>>>>>> Stashed changes - - - \ No newline at end of file diff --git a/.idea/rGraphicsSettings.xml b/.idea/rGraphicsSettings.xml deleted file mode 100644 index b5b9c2e..0000000 --- a/.idea/rGraphicsSettings.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/rSettings.xml b/.idea/rSettings.xml deleted file mode 100644 index ca29c32..0000000 --- a/.idea/rSettings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..5657272 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,94 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1701020308079 + + + + + + + + + \ No newline at end of file diff --git a/ha_03/loosen_janniclas_1540907_04.py b/ha_03/loosen_janniclas_1540907_04.py index a311ea7..904cdc5 100644 --- a/ha_03/loosen_janniclas_1540907_04.py +++ b/ha_03/loosen_janniclas_1540907_04.py @@ -1,4 +1,3 @@ -<<<<<<< Updated upstream # Jan-Niclas Loosen # 1540907 @@ -32,170 +31,4 @@ num = int(input("insert positive integer: ")) if num < 0 : print("invalid input") else: - print(prime_factorization(num)) -======= -# Author: Jan-Niclas Loosen -# Matrikelnummer: 1540907 -import math -import numpy as np -import matplotlib.pyplot as plt - - -# EXERCISE 1 -class Rational: - numerator = 0 - divisor = 1 - - def __str__(self): - return "%d/%d" % (self.numerator, self.divisor) - - def __init__(self, a=0, b=1): - if isinstance(a, Rational): - self.numerator = a.numerator - self.divisor = a.divisor - self.shorten() - elif isinstance(a, int) and isinstance(b, int): - if b == 0: - print("Divisor cannot be zero!") - exit(1) - if b < 0: - b = -b - a = -a - self.numerator = a - self.divisor = b - self.shorten() - else: - print("Numerator and divisor must be integer.") - exit(1) - - def gcd(self, a, b): - while b != 0: - (a, b) = (b, a % b) - return a - - def lcm(self, a, b): - return abs(a * b) // self.gcd(a, b) - - def lcd(self, other): - if not isinstance(other, Rational): - other = Rational(other) - return self.lcm(self.divisor, other.divisor) - - def shorten(self): - d = self.gcd(self.numerator, self.divisor) - self.numerator = self.numerator // d - self.divisor = self.divisor // d - - # addition - def __add__(self, other): - if not isinstance(other, Rational): - other = Rational(other) - - lcm = self.lcm(self.divisor, other.divisor) - - fac1 = lcm // self.divisor - fac2 = lcm // other.divisor - - new_divisor = lcm - new_nominator = self.numerator * fac1 + other.numerator * fac2 - return Rational(new_nominator, new_divisor) - - def __radd__(self, other): - return self.__add__(other) - - # subtraction - def __sub__(self, other): - if not isinstance(other, Rational): - other = Rational(other) - divisor_lcm = self.lcm(self.divisor, other.divisor) - new_nominator = (self.numerator * divisor_lcm / self.divisor) - (other.numerator * divisor_lcm / other.divisor) - return Rational(int(new_nominator), int(divisor_lcm)) - - def __rsub__(self, other): - if not isinstance(other, Rational): - other = Rational(other) - divisor_lcm = self.lcm(self.divisor, other.divisor) - new_nominator = (other.numerator * divisor_lcm / other.divisor) - (self.numerator * divisor_lcm / self.divisor) - return Rational(int(new_nominator), int(divisor_lcm)) - - # multiplication - def __mul__(self, other): - if isinstance(other, Rational): - return Rational(self.numerator * other.numerator, self.divisor * other.divisor) - else: - return Rational(self.numerator * other, self.divisor) - - def __rmul__(self, other): - return self.__mul__(other) - - # division - def __truediv__(self, other): - if not isinstance(other, Rational): - other = Rational(other) - new_numerator = self.numerator * other.divisor - new_divisor = self.divisor * other.numerator - return Rational(new_numerator, new_divisor) - - def __rtruediv__(self, other): - if not isinstance(other, Rational): - other = Rational(other) - new_numerator = self.divisor * other.numerator - new_divisor = self.numerator * other.divisor - return Rational(new_numerator, new_divisor) - - def __lt__(self, other): - divisor_lcm = self.lcd(other) - return (self.numerator * divisor_lcm / self.divisor) < (other.numerator * divisor_lcm / other.divisor) - - def __gt__(self, other): - divisor_lcm = self.lcd(other) - return (self.numerator * divisor_lcm / self.divisor) > (other.numerator * divisor_lcm / other.divisor) - - def __le__(self, other): - divisor_lcm = self.lcd(other) - return (self.numerator * divisor_lcm / self.divisor) <= (other.numerator * divisor_lcm / other.divisor) - - def __ge__(self, other): - divisor_lcm = self.lcd(other) - return (self.numerator * divisor_lcm / self.divisor) >= (other.numerator * divisor_lcm / other.divisor) - - def __eq__(self, other): - if not isinstance(other, Rational): - other = Rational(other) - # all constructed Rationals are already shortened - return self.divisor == other.divisor and self.numerator == other.numerator - - def __ne__(self, other): - return not self.__eq__(other) - - -# EXERCISE 2 -def heron(a, x, run): - for i in range(run): - x = x - (x ** 2 - a) / (2 * x) - return x - - -# Generate x values -x_values = [x for x in range(10)] - -# Calculate the difference between heron and sqrt functions -a = 2 -b = 1.9 -x0 = 1.5 - -y_values_a = [abs(heron(a, x0, run) - np.sqrt(a)) for run in x_values] -y_values_b = [abs(heron(b, x0, run) - np.sqrt(b)) for run in x_values] - -# Plot the difference -plt.plot(x_values, y_values_a, label='a = 2, x0 = 1.5', color="blue") -plt.plot(x_values, y_values_b, label='b = 0.5, x0 = 1.5', color="red") - -# Add labels and a legend -plt.xlabel('Iterations') -plt.ylabel('Difference') -plt.legend() - -# Show the plot -plt.show() ->>>>>>> Stashed changes + print(prime_factorization(num)) \ No newline at end of file diff --git a/ha_04/loosen_janniclas_1540907_05.py b/ha_04/loosen_janniclas_1540907_05.py new file mode 100644 index 0000000..2de1eed --- /dev/null +++ b/ha_04/loosen_janniclas_1540907_05.py @@ -0,0 +1,163 @@ +# Author: Jan-Niclas Loosen +# Matrikelnummer: 1540907 +import numpy as np +import matplotlib.pyplot as plt + + +# EXERCISE 1 +class Rational: + numerator = 0 + divisor = 1 + + def __str__(self): + return "%d/%d" % (self.numerator, self.divisor) + + def __init__(self, a=0, b=1): + if isinstance(a, Rational): + self.numerator = a.numerator + self.divisor = a.divisor + self.shorten() + elif isinstance(a, int) and isinstance(b, int): + if b == 0: + print("Divisor cannot be zero!") + exit(1) + if b < 0: + b = -b + a = -a + self.numerator = a + self.divisor = b + self.shorten() + else: + print("Numerator and divisor must be integer.") + exit(1) + + def gcd(self, a, b): + while b != 0: + (a, b) = (b, a % b) + return a + + def lcm(self, a, b): + return abs(a * b) // self.gcd(a, b) + + def lcd(self, other): + if not isinstance(other, Rational): + other = Rational(other) + return self.lcm(self.divisor, other.divisor) + + def shorten(self): + d = self.gcd(self.numerator, self.divisor) + self.numerator = self.numerator // d + self.divisor = self.divisor // d + + # addition + def __add__(self, other): + if not isinstance(other, Rational): + other = Rational(other) + + lcm = self.lcm(self.divisor, other.divisor) + + fac1 = lcm // self.divisor + fac2 = lcm // other.divisor + + new_divisor = lcm + new_nominator = self.numerator * fac1 + other.numerator * fac2 + return Rational(new_nominator, new_divisor) + + def __radd__(self, other): + return self.__add__(other) + + # subtraction + def __sub__(self, other): + if not isinstance(other, Rational): + other = Rational(other) + divisor_lcm = self.lcm(self.divisor, other.divisor) + new_nominator = (self.numerator * divisor_lcm / self.divisor) - (other.numerator * divisor_lcm / other.divisor) + return Rational(int(new_nominator), int(divisor_lcm)) + + def __rsub__(self, other): + if not isinstance(other, Rational): + other = Rational(other) + divisor_lcm = self.lcm(self.divisor, other.divisor) + new_nominator = (other.numerator * divisor_lcm / other.divisor) - (self.numerator * divisor_lcm / self.divisor) + return Rational(int(new_nominator), int(divisor_lcm)) + + # multiplication + def __mul__(self, other): + if isinstance(other, Rational): + return Rational(self.numerator * other.numerator, self.divisor * other.divisor) + else: + return Rational(self.numerator * other, self.divisor) + + def __rmul__(self, other): + return self.__mul__(other) + + # division + def __truediv__(self, other): + if not isinstance(other, Rational): + other = Rational(other) + new_numerator = self.numerator * other.divisor + new_divisor = self.divisor * other.numerator + return Rational(new_numerator, new_divisor) + + def __rtruediv__(self, other): + if not isinstance(other, Rational): + other = Rational(other) + new_numerator = self.divisor * other.numerator + new_divisor = self.numerator * other.divisor + return Rational(new_numerator, new_divisor) + + def __lt__(self, other): + divisor_lcm = self.lcd(other) + return (self.numerator * divisor_lcm / self.divisor) < (other.numerator * divisor_lcm / other.divisor) + + def __gt__(self, other): + divisor_lcm = self.lcd(other) + return (self.numerator * divisor_lcm / self.divisor) > (other.numerator * divisor_lcm / other.divisor) + + def __le__(self, other): + divisor_lcm = self.lcd(other) + return (self.numerator * divisor_lcm / self.divisor) <= (other.numerator * divisor_lcm / other.divisor) + + def __ge__(self, other): + divisor_lcm = self.lcd(other) + return (self.numerator * divisor_lcm / self.divisor) >= (other.numerator * divisor_lcm / other.divisor) + + def __eq__(self, other): + if not isinstance(other, Rational): + other = Rational(other) + # all constructed Rationals are already shortened + return self.divisor == other.divisor and self.numerator == other.numerator + + def __ne__(self, other): + return not self.__eq__(other) + + +# EXERCISE 2 +def heron(a, x, run): + for i in range(run): + x = x - (x ** 2 - a) / (2 * x) + return x + + +# Generate x values +x_values = [x for x in range(10)] + +# Calculate the difference between heron and sqrt functions +a = 2 +b = 1.9 +x0 = 1.5 + +y_values_a = [abs(heron(a, x0, run) - np.sqrt(a)) for run in x_values] +y_values_b = [abs(heron(b, x0, run) - np.sqrt(b)) for run in x_values] + +# Plot the difference +plt.plot(x_values, y_values_a, label='a = 2, x0 = 1.5', color="blue") +plt.plot(x_values, y_values_b, label='b = 0.5, x0 = 1.5', color="red") + +# Add labels and a legend +plt.xlabel('Iterations') +plt.ylabel('Difference') +plt.legend() + +# Show the plot +plt.show()