diff --git a/ex1 b/ex1 new file mode 100644 index 0000000..3c2b3a1 --- /dev/null +++ b/ex1 @@ -0,0 +1 @@ +print(sum([x for x in range(1,1000) if ((x%5==0) or (x%3==0))])) diff --git a/ex2 b/ex2 new file mode 100644 index 0000000..c2370cb --- /dev/null +++ b/ex2 @@ -0,0 +1,21 @@ +#Cada término en la serie de Fibonacci es generado a partir de la suma de los dos términos previos, empezando de 1 y 2, los diez primeros #términos serán: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … Considerando los términos de la serie de Fibonacci que son impares, y por debajo de un #millón encuentre la suma de dichos términos. + + +def fib(n): + f=[1,2] + if n==1: + return f[0] + if n==2: + return [1,2] + + for k in range(3,n+1): + f.append(f[k-2]+f[k-3]) + return f + +for i in range(3,40): + if fib(i)[-1]>1000000: + lim=i + break + +print(sum([x for x in fib(lim-1) if x%2==1])) +print(fib(30)) diff --git a/ex3 b/ex3 new file mode 100644 index 0000000..9f08b3e --- /dev/null +++ b/ex3 @@ -0,0 +1,10 @@ +def prim_factors(n): + factors=[] + for i in range(2,n): + if (n%i==0): + n=n//i + factors.append(i) + + return factors # [x for x in factors if prim_factors(x)==[]] + +print(prim_factors(600851475143)) diff --git a/ex3.py b/ex3.py new file mode 100644 index 0000000..208678a --- /dev/null +++ b/ex3.py @@ -0,0 +1,15 @@ +def prime_factors(n): + factors=[] + i=2 + while i*i<=n: + #print(i,n) + if (n%i==0): + n=n//i + factors.append(i) + i=2 + else: + i+=1 + factors.append(n) + return sorted(list(set(factors)))[-1] + +print(prime_factors(600851475143)) diff --git a/ex4.py b/ex4.py new file mode 100644 index 0000000..15f1fff --- /dev/null +++ b/ex4.py @@ -0,0 +1,16 @@ +import matplotlib.pyplot as plt +import numpy as np + + +data=np.loadtxt('lista.txt',delimiter=' ') +x=data.T[0] +y=data.T[1] + + +plt.scatter(x,y) +plt.xlabel('Eje x') +plt.ylabel('Eje y') +for i, txt in enumerate(zip(x,y)): + plt.annotate(txt, (x[i], y[i])) +plt.show() + diff --git a/ex6.py b/ex6.py new file mode 100644 index 0000000..1cc9f18 --- /dev/null +++ b/ex6.py @@ -0,0 +1,32 @@ +import numpy as np +import matplotlib.pyplot as plt +from scipy.optimize import curve_fit + +def fitFunc(t, a, b, c): + return a*t**2+b*t+c + +data=np.loadtxt('lista.txt',delimiter=' ') +t=data.T[0] +y=data.T[1] + + +fitParams, fitCovariances = curve_fit(fitFunc, t, y) +print(fitParams) +print(fitCovariances) + +plt.ylabel('Temperature (C)', fontsize = 16) +plt.xlabel('time (s)', fontsize = 16) +#plt.xlim(0,4.1) +# plot the data as red circles with errorbars in the vertical direction +plt.errorbar(t, y, fmt = 'ro', yerr = 0.2) +# now plot the best fit curve and also +- 3 sigma curves +# the square root of the diagonal covariance matrix element +# is the uncertianty on the corresponding fit parameter. +sigma = [fitCovariances[0,0], fitCovariances[1,1], fitCovariances[2,2] ] +plt.plot(t, fitFunc(t, fitParams[0], fitParams[1], fitParams[2]),\ + t, fitFunc(t, fitParams[0] + sigma[0], fitParams[1] - sigma[1], fitParams[2] + sigma[2]),\ + t, fitFunc(t, fitParams[0] - sigma[0], fitParams[1] + sigma[1], fitParams[2] - sigma[2])\ + ) +plt.show() +# save plot to a file +#savefig('dataFitted.pdf', bbox_inches=0, dpi=600) diff --git a/ex7.py b/ex7.py new file mode 100644 index 0000000..f4ce0e0 --- /dev/null +++ b/ex7.py @@ -0,0 +1,29 @@ +import matplotlib.pyplot as plt +import numpy as np + +def pol(x): + return x**3+x**2-4*x+4 + +#def der(x): +# return 3*x**2+2*x-4 + + +x=np.arange(-10,10,0.1) +y=[pol(t) for t in x] +z= np.diff(y) +print(z) +plt.plot(x,y) +plt.plot(x,z,color='red') +plt.xlabel('Eje x') +plt.ylabel('Eje y') +plt.grid() +plt.legend(['Polinomio','Derivada']) +plt.show() + +print(np.c_[x, y].shape) +#with open('pol_data.txt','w') as file: +file= open('pol_data.txt','w') +np.savetxt(file, np.c_[x, y], fmt='%f', delimiter='\t', header="x #f(x)") + +file.close() + diff --git a/lista.txt b/lista.txt new file mode 100644 index 0000000..57c72b6 --- /dev/null +++ b/lista.txt @@ -0,0 +1,10 @@ +7.5 28.66 +4.48 20.37 +8.60 22.33 +7.73 26.35 +5.28 22.29 +4.25 21.74 +6.99 23.11 +6.31 23.13 +9.15 24.68 +5.06 21.89