-
Notifications
You must be signed in to change notification settings - Fork 1
/
Puntos_dentro_del_circulo.hs
76 lines (61 loc) · 2.06 KB
/
Puntos_dentro_del_circulo.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
-- Puntos_dentro_del_circulo.hs
-- Puntos dentro del círculo.
-- José A. Alonso Jiménez <https://jaalonso.github.io>
-- Sevilla, 11-octubre-2022
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
-- En el círculo de radio 2 hay 6 puntos cuyas coordenadas son puntos
-- naturales:
-- (0,0),(0,1),(0,2),(1,0),(1,1),(2,0)
-- y en de radio 3 hay 11:
-- (0,0),(0,1),(0,2),(0,3),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2),(3,0)
--
-- Definir la función
-- circulo :: Int -> Int
-- tal que (circulo n) es el la cantidad de pares de números naturales
-- (x,y) que se encuentran en el círculo de radio n. Por ejemplo,
-- circulo 1 == 3
-- circulo 2 == 6
-- circulo 3 == 11
-- circulo 4 == 17
-- circulo 100 == 7955
-- ---------------------------------------------------------------------
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
module Puntos_dentro_del_circulo where
import Test.QuickCheck
-- 1ª solución
-- ===========
circulo1 :: Int -> Int
circulo1 n = length (enCirculo1 n)
enCirculo1 :: Int -> [(Int, Int)]
enCirculo1 n = [(x,y) | x <- [0..n],
y <- [0..n],
x*x+y*y <= n*n]
-- 2ª solución
-- ===========
circulo2 :: Int -> Int
circulo2 0 = 1
circulo2 n =
2 * length (enSemiCirculo n) + ceiling(fromIntegral n / sqrt 2)
enSemiCirculo :: Int -> [(Int, Int)]
enSemiCirculo n =
[(x,y) | x <- [0..floor (sqrt (fromIntegral (n * n)))],
y <- [x+1..truncate (sqrt (fromIntegral (n*n - x*x)))]]
-- Comprobación de equivalencia
-- ============================
-- La propiedad es
prop_circulo :: Positive Int -> Bool
prop_circulo (Positive n) =
circulo1 n == circulo2 n
-- La comprobación es
-- λ> quickCheck prop_circulo
-- +++ OK, passed 100 tests.
-- Comparación de eficiencia
-- =========================
-- La comparación es
-- λ> circulo1 (2*10^3)
-- 3143587
-- (3.58 secs, 1,744,162,600 bytes)
-- λ> circulo2 (2*10^3)
-- 3143587
-- (0.41 secs, 266,374,208 bytes)