|
|
|
<<<<<<< Updated upstream
|
|
|
|
# Jan-Niclas Loosen
|
|
|
|
# 1540907
|
|
|
|
|
|
|
|
def sieve_of_eratosthenes(limit):
|
|
|
|
primes = [True] * (limit + 1)
|
|
|
|
primes[0] = primes[1] = False # 0 and 1 are not primes
|
|
|
|
|
|
|
|
for num in range(2, int(limit ** 0.5) + 1): # only necessary to the square root of the limit
|
|
|
|
if primes[num]:
|
|
|
|
prime = num
|
|
|
|
for i in range(1,limit):
|
|
|
|
mult = prime * (i + 1)
|
|
|
|
if mult >= len(primes): # more loops are unnecessary
|
|
|
|
break
|
|
|
|
primes[mult] = False
|
|
|
|
|
|
|
|
return primes
|
|
|
|
|
|
|
|
def prime_factorization(n):
|
|
|
|
primes = sieve_of_eratosthenes(n)
|
|
|
|
factors = []
|
|
|
|
for i in range(2, n + 1):
|
|
|
|
if primes[i] and n % i == 0:
|
|
|
|
while n % i == 0:
|
|
|
|
factors.append(i)
|
|
|
|
n = n // i
|
|
|
|
return factors
|
|
|
|
|
|
|
|
# dialog for testing the functions
|
|
|
|
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
|