main
Jan-Niclas Loosen 10 months ago
parent f64a9186b5
commit 9bf0971627

@ -1,4 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<<<<<<< Updated upstream
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10.6 WSL (Ubuntu): (/home/janniclas/.virtualenvs/Informationssysteme/bin/python)" project-jdk-type="Python SDK" />
=======
<component name="ProjectRootManager" version="2" project-jdk-name="C:\ProgramData\anaconda3 (2)" project-jdk-type="Python SDK" />
>>>>>>> Stashed changes
</project>

@ -5,7 +5,11 @@
<excludeFolder url="file://$MODULE_DIR$/Virtualenv" />
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<<<<<<< Updated upstream
<orderEntry type="jdk" jdkName="Python 3.10.6 WSL (Ubuntu): (/home/janniclas/.virtualenvs/Informationssysteme/bin/python)" jdkType="Python SDK" />
=======
<orderEntry type="jdk" jdkName="C:\ProgramData\anaconda3 (2)" jdkType="Python SDK" />
>>>>>>> Stashed changes
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RGraphicsSettings">
<option name="height" value="576" />
<option name="resolution" value="75" />
<option name="version" value="2" />
<option name="width" value="2048" />
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RSettings">
<option name="interpreterPath" value="C:\Program Files\R\R-4.3.2\bin\R.exe" />
</component>
</project>

@ -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

Loading…
Cancel
Save