Menu Close

Elementos múltiplos de la longitud de la lista

Definir las funciones

   multiplosDeLaLongitud :: [Int] -> [Int]
   multiplosDeLaLongitudDeConsecutivos :: Int -> Int -> [Int]

tales que

  • (multiplosDeLaLongitud xs) es la lista de los elementos de xs que son múltiplos de la longitud de xs. Por ejemplo,
     multiplosDeLaLongitud [2,4,6,8] == [4,8]
  • (multiplosDeLaLongitudDeConsecutivos n m) es la lista de elementos de [n..n+m-1] que son múltiplos de n. Por ejemplo,
     multiplosDeLaLongitudDeConsecutivos 9 2  ==  [10]
     multiplosDeLaLongitudDeConsecutivos 9 12 ==  [12]

Comprobar con QuickCheck si se verifican las siguientes propiedades

  • En cualquier conjunto de m elementos consecutivos, m divide exactamente a uno de dichos elementos. En otras palabras, si n y m son enteros positivos, entonces (multiplosDeLaLongitudDeConsecutivos n m) tiene exactamente un elemento.
  • Si n es un entero positivo y m >= n, entonces (multiplosDeLaLongitudDeConsecutivos n m) es igual a [m]
  • Si n y n son enteros positivos y m < n, entonces (multiplosDeLaLongitudDeConsecutivos n m) es igual a [m * ceiling (n’ / m’)] donde n’ y m’ son las formas decimales de n y m respectivamente.

Soluciones

import Test.QuickCheck
 
multiplosDeLaLongitud :: [Int] -> [Int]
multiplosDeLaLongitud xs =
  [x | x <- xs
     , x `mod`  n == 0]
  where n = length xs
 
multiplosDeLaLongitudDeConsecutivos :: Int -> Int -> [Int]
multiplosDeLaLongitudDeConsecutivos n m =
  multiplosDeLaLongitud [n..n+m-1]
 
-- La 1ª propiedad es
prop_multiplosDeLaLongitud :: Int -> Int -> Property
prop_multiplosDeLaLongitud n m =
  n > 0 && m > 0
  ==>
  length (multiplosDeLaLongitudDeConsecutivos n m) == 1
 
-- La comprobación es
--    λ> quickCheck prop_multiplosDeLaLongitud
--    +++ OK, passed 100 tests.
 
-- La 2ª propiedad es
prop_multiplosDeLaLongitud2 :: Int -> Int -> Property
prop_multiplosDeLaLongitud2 n m =
  n > 0 && m >= n
  ==>
  multiplosDeLaLongitudDeConsecutivos n m == [m]
 
-- La comprobación es
--    λ> quickCheck prop_multiplosDeLaLongitud2
--    +++ OK, passed 100 tests.
 
-- La 3ª propiedad es
prop_multiplosDeLaLongitud3 :: Int -> Int -> Property
prop_multiplosDeLaLongitud3 n m =
  n > 0 && 0 < m && m < n
  ==>
  multiplosDeLaLongitudDeConsecutivos n m == [m * ceiling (n' / m')]
  where n' = fromIntegral n
        m' = fromIntegral m 
 
-- La comprobación es
--    λ> quickCheck prop_multiplosDeLaLongitud3
--    +++ OK, passed 100 tests.
 
-- Con las propiedades anteriores se puede redefinir multiplos
multiplosDeLaLongitudDeConsecutivos2 :: Int -> Int -> [Int]
multiplosDeLaLongitudDeConsecutivos2 n m
  | m < n     = [m * ceiling (n' / m')]
  | otherwise = [m]
  where n' = fromIntegral n
        m' = fromIntegral m 
 
-- Comprobación de la equivalencia
prop_equiv :: Int -> Int -> Property
prop_equiv n m =
  n > 0 && m > 0
  ==>
  multiplosDeLaLongitudDeConsecutivos  n m ==
  multiplosDeLaLongitudDeConsecutivos2 n m
 
-- La comprobación es
--    λ> quickCheck prop_multiplosDeLaLongitud
--    +++ OK, passed 100 tests.
 
-- Comparación de eficiencia
-- =========================
 
--    λ> xs1 = [multiplosDeLaLongitudDeConsecutivos n (10^3) | n <- [1..10^4]]
--    (0.01 secs, 0 bytes)
--    λ> xs2 = [multiplosDeLaLongitudDeConsecutivos2 n (10^3) | n <- [1..10^4]]
--    (0.01 secs, 0 bytes)
--    λ> maximum xs1
--    [10000]
--    (2.51 secs, 2,093,241,168 bytes)
--    λ> maximum xs2
--    [10000]
--    (0.02 secs, 16,742,408 bytes)

Referencia

Pensamiento

Pensando que no veía
porque Dios no le miraba,
dijo Abel cuando moría:
Se acabó lo que se daba.

Antonio Machado

Posted in Medio

2 Comments

  1. rebgongor
    import Test.QuickCheck
     
    multiplosDeLaLongitud :: [Int] -> [Int]
    multiplosDeLaLongitud xs =
      [x | x <- xs
         , mod x (length xs) == 0]
     
    multiplosDeLaLongitudConsecutivos :: Int -> Int -> [Int]
    multiplosDeLaLongitudConsecutivos n m =
      [x | x <- [n..n+m-1]
         , mod x m == 0]
     
    propiedad_1 :: Int -> Int -> Property
    propiedad_1 n m =
      n > 0 && m > 0
      ==>
      length (multiplosDeLaLongitudConsecutivos n m) == 1
     
    -- λ> quickCheck propiedad_1
    -- +++ OK, passed 100 tests.
     
    propiedad_2 :: Int -> Int -> Property
    propiedad_2 n m =
      n > 0 && m >= n
      ==>
      multiplosDeLaLongitudConsecutivos n m == [m]
     
    -- λ> quickCheck propiedad_2
    -- +++ OK, passed 100 tests.
     
    propiedad_3 :: Int -> Int -> Property
    propiedad_3 n m =
      n > 0 && m > 0 && m < n
      ==>
      multiplosDeLaLongitudConsecutivos n m == [m * ceiling (n'/m')]
      where n' = fromIntegral n
            m' = fromIntegral m
     
    -- λ> quickCheck propiedad_3
    -- +++ OK, passed 100 tests.
  2. fercarnav
    import Test.QuickCheck
     
    multiplosDeLaLongitud :: [Int] -> [Int]
    multiplosDeLaLongitud xs = filter ((==0). (`mod` (length xs))) xs
     
    multiplosDeLaLongitudDeConsecutivos :: Int -> Int -> [Int]
    multiplosDeLaLongitudDeConsecutivos n m =
      multiplosDeLaLongitud [n,n+1..n+m-1]
     
    propConsecutivos :: Int -> Int -> Property
    propConsecutivos n m =
      n > 0 && m > 0 ==>
      length (multiplosDeLaLongitudDeConsecutivos n m ) == 1
     
    propConsecutivos2 :: Int -> Int -> Property
    propConsecutivos2 n m =
      m >= n && n > 0 ==>
      (multiplosDeLaLongitudDeConsecutivos n m ) == [m]
     
    propConsecutivos3 :: Int -> Int -> Property
    propConsecutivos3 n m =
      m < n && m > 0 ==>
      (multiplosDeLaLongitudDeConsecutivos n m ) == [m * ceiling (n2 / m2)]
      where n2 = fromIntegral n
            m2 = fromIntegral m
     
    -- λ> quickCheck propConsecutivos
    -- +++ OK, passed 100 tests.
    -- λ> quickCheck propConsecutivos2
    -- +++ OK, passed 100 tests.
    -- λ> quickCheck propConsecutivos3
    -- +++ OK, passed 100 tests.

Escribe tu solución

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.