-
Notifications
You must be signed in to change notification settings - Fork 1
/
Solucion.hs
231 lines (185 loc) · 6.55 KB
/
Solucion.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
-- [1]
fibonacci :: Integer -> Integer
fibonacci 0 = 1
fibonacci 1 = 1
fibonacci n = fibonacci(n-2) + fibonacci(n-1)
-- [2]
parteEnteraPositiva :: Float -> Integer
parteEnteraPositiva n | (0 <= n) && (n < 1) = 0
| otherwise = 1 + parteEnteraPositiva(n-1)
parteEnteraNegativa :: Float -> Integer
parteEnteraNegativa n | (-1 <= n) && (n < 0) = -1
| otherwise = -1 - parteEnteraNegativa (n+1)
parteEntera :: Float -> Integer
parteEntera n | (n >= 0) = parteEnteraPositiva n
| otherwise = parteEnteraNegativa n
-- [3]
{-
esDivisible :: Integer -> Integer -> Bool
esDivisible a b = (mod a b == 0)
o tambien,
esDivisible a b = parteEntera (a/b) == a/b
-}
-- [4]
sumaImpares :: Integer -> Integer
sumaImpares 1 = 1
sumaImpares n = sumaImpares (n-1) + impar (n)
impar :: Integer -> Integer
impar 1 = 1
impar n = impar (n-1) + 2
--[5]
medioFact :: Integer -> Integer
medioFact 0 = 0
medioFact 1 = 1
medioFact 2 = 2
medioFact n = n * medioFact (n-2)
--[6]
sumaDigitos :: Integer -> Integer
sumaDigitos n | (cantidadDigitos n == 1) = n
| otherwise = ultimoDigito n + sumaDigitos (sacarUltimo n)
where sacarUltimo n = div n 10
cantidadDigitos :: Integer -> Integer
cantidadDigitos n | n < 10 = 1
| otherwise = 1 + cantidadDigitos (div n 10)
--[7] (copiar el mas clean_code de la guia resuelta)
todosDigitosIguales :: Integer -> Bool
todosDigitosIguales n | abs(n) < 10 = True
| (ultimoDigito n == segundoUltimoDigito n) = todosDigitosIguales (div n 10)
| otherwise = False
ultimoDigito :: Integer -> Integer
ultimoDigito n | n < 10 = n
| otherwise = mod n 10
segundoUltimoDigito :: Integer -> Integer
segundoUltimoDigito n = mod (div n 10) 10
--[8]
iesimoDigito :: Integer -> Integer -> Integer
iesimoDigito n i | (cantidadDigitos n == i) = ultimoDigito (n)
| otherwise = iesimoDigito (sacarUltimo n) i
where sacarUltimo n = div n 10
ultimoDigito n = mod n 10
--[9]
esCapicua :: Integer -> Bool
esCapicua n | (n < 10) = True
| (invertir (n) == n) = True
| otherwise = False
invertir :: Integer -> Integer
invertir n | (cantidadDigitos (n) == 1) = n
| otherwise = (ultimoDigito (n) * 10^(cantidadDigitos(n)-1)) + invertir (sacarUltimo (n))
where ultimoDigito n = mod n 10
sacarUltimo n = div n 10
--[10]
f1 :: Integer -> Integer
f1 0 = 1
f1 n = f1 (n-1) + 2^n
f2 :: Integer -> Integer -> Integer
f2 0 _ = 0
f2 _ 0 = 0
f2 n q = f2 (n-1) q + q^n
f3 :: Integer -> Integer -> Integer
f3 0 _ = 0
f3 _ 0 = 0
f3 n q = f2 (2*n) q
f4 :: Integer -> Integer -> Integer
f4 n q = f3 n q - f2 n q
--[11]
eAprox :: Integer -> Float
eAprox 0 = 1
eAprox n = eAprox (n-1) + 1.0 / fromInteger (factorial (n))
e = eAprox 10
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = factorial (n-1) * n
--[12]
raizDe2Aprox :: Integer -> Float
raizDe2Aprox n = a (n) - 1
a :: Integer -> Float
a 1 = 2
a n = 2 + 1 / a (n-1)
--[13]
f :: Integer -> Integer -> Integer
f 1 m = m
f n 1 = div (n * (n+1)) 2
f n m = (f (n-1) m) + n^m
--[14]
sumaPotencias :: Integer -> Integer -> Integer -> Float
sumaPotencias q m n = fromInteger ((f2 m q) * (f2 n q))
--[15]
{-
sumaRacionales :: Integer -> Integer -> Float
sumaRacionales n m = (n*(n+1)/2) * suma_fracc m
suma_fracc :: Integer -> Float
suma_fracc 1 = 1
suma_fracc m = suma_fracc (m-1) + 1/m
-}
--[16]
--(A)
menorDivisor :: Integer -> Integer
menorDivisor n = menorDivisorDesde n 2
menorDivisorDesde :: Integer -> Integer -> Integer
menorDivisorDesde n k | (mod n k == 0) = k
| otherwise = menorDivisorDesde n (k+1)
--(B)
esPrimo :: Integer -> Bool
esPrimo 1 = False
esPrimo n = (menorDivisor n == n)
--(C)
sonCoprimos :: Integer -> Integer -> Bool
sonCoprimos a b = (mcd a b == 1)
mcd :: Integer -> Integer -> Integer
mcd x 0 = x
mcd a b = mcd b (mod a b)
--(D)
nEsimoPrimo :: Integer -> Integer
nEsimoPrimo 1 = 2
nEsimoPrimo k = proximoPrimoDesde (nEsimoPrimo (k-1))
proximoPrimoDesde :: Integer -> Integer
proximoPrimoDesde n | esPrimo (n+1) = n + 1
| otherwise = proximoPrimoDesde (n+1)
--[17]
esFibonacci :: Integer -> Bool
esFibonacci n = esFibonacciDesde n 1
esFibonacciDesde :: Integer -> Integer -> Bool
esFibonacciDesde n k | (fibonacci(k) == n) = True
| (fibonacci(k) > n) = False
| (fibonacci(k) < n) = esFibonacciDesde n (k+1)
--[18]
mayorDigitoPar :: Integer -> Integer
mayorDigitoPar n | (n < 10) && (mod n 2 /= 0) = -1
| (n < 10) && (mod n 2 == 0) = n
| (ultimoDigito n >= mayorDigitoPar (sacarUltimo n)) && (mod (ultimoDigito n) 2 == 0) = ultimoDigito n
| (ultimoDigito n < mayorDigitoPar (sacarUltimo n)) = mayorDigitoPar (sacarUltimo n)
| otherwise = mayorDigitoPar (sacarUltimo n)
where sacarUltimo n = div n 10
--[19]
esSumaInicialDePrimos :: Integer -> Bool
esSumaInicialDePrimos n = esSumaInicialDePrimosDesde n 1
esSumaInicialDePrimosDesde :: Integer -> Integer -> Bool
esSumaInicialDePrimosDesde n i | sumaKprimos (i) > n = False
| sumaKprimos (i) < n = esSumaInicialDePrimosDesde n (i+1)
| otherwise = True
sumaKprimos :: Integer -> Integer
sumaKprimos 1 = 2
sumaKprimos n = sumaKprimos (n-1) + nEsimoPrimo n
--[20]
tomaValorMax :: Integer -> Integer -> Integer
tomaValorMax n m | (n == m) = sumaDivisores(n)
| otherwise = max (sumaDivisores (n)) (tomaValorMax (n+1) m)
sumaDivisores :: Integer -> Integer
sumaDivisores n = sumaDivisoresDesde n 1
sumaDivisoresDesde :: Integer -> Integer -> Integer
sumaDivisoresDesde n k | (k == n) = n
| (k < n) && (mod n k == 0) = k + sumaDivisoresDesde n (k+1)
| (k > n) = 0
| otherwise = sumaDivisoresDesde n (k+1)
--[21]
pitagoras :: Integer -> Integer -> Integer -> Integer
pitagoras 0 n r = cantidadXmenorY n (r^2)
pitagoras m 0 r = cantidadXmenorY m (r^2)
pitagoras m n r = pitagoras (m-1) n r + cantidadXmenorY n (r^2 - m^2)
cantidadXmenorY :: Integer -> Integer -> Integer
cantidadXmenorY x y = cantidadXmenorYDesde x y 0
cantidadXmenorYDesde :: Integer -> Integer -> Integer -> Integer
cantidadXmenorYDesde x y k | (k < x) && (k^2 <= y) = 1 + cantidadXmenorYDesde x y (k+1)
| (k < x) = cantidadXmenorYDesde x y (k+1)
| (k == x) && (k^2 <= y) = 1
| (k == x) = 0