From 5dc23d5184d22d2899bb734c5961f94de0de2320 Mon Sep 17 00:00:00 2001 From: Jan-Niclas Loosen Date: Sun, 26 Nov 2023 18:35:23 +0100 Subject: [PATCH] 26/11/2023 revert revert 26/11/2023 revert 26/11/2023 --- .idea/misc.xml | 4 + .idea/python.iml | 4 + .idea/rGraphicsSettings.xml | 9 ++ .idea/rSettings.xml | 6 + ha_03/loosen_janniclas_1540907_04.py | 167 +++++++++++++++++++++++++++ 5 files changed, 190 insertions(+) create mode 100644 .idea/rGraphicsSettings.xml create mode 100644 .idea/rSettings.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 46ad6d9..7489572 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,8 @@ +<<<<<<< Updated upstream +======= + +>>>>>>> Stashed changes \ No newline at end of file diff --git a/.idea/python.iml b/.idea/python.iml index ca820fc..64b45c5 100644 --- a/.idea/python.iml +++ b/.idea/python.iml @@ -5,7 +5,11 @@ +<<<<<<< Updated upstream +======= + +>>>>>>> Stashed changes \ No newline at end of file diff --git a/.idea/rGraphicsSettings.xml b/.idea/rGraphicsSettings.xml new file mode 100644 index 0000000..b5b9c2e --- /dev/null +++ b/.idea/rGraphicsSettings.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/.idea/rSettings.xml b/.idea/rSettings.xml new file mode 100644 index 0000000..ca29c32 --- /dev/null +++ b/.idea/rSettings.xml @@ -0,0 +1,6 @@ + + + + + \ 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 9db67f6..a311ea7 100644 --- a/ha_03/loosen_janniclas_1540907_04.py +++ b/ha_03/loosen_janniclas_1540907_04.py @@ -1,3 +1,4 @@ +<<<<<<< Updated upstream # Jan-Niclas Loosen # 1540907 @@ -32,3 +33,169 @@ 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