Rotaciones divisibles por 4

Las rotaciones de 928160 son 928160, 281609, 816092, 160928, 609281 y 92816. De las cuales, las divisibles por 4 son 928160, 816092, 160928 y 92816.

Definir la función

tal que (nRotacionesDivisibles n) es el número de rotaciones del número n divisibles por 4. Por ejemplo,

Soluciones

15 Comentarios

  1. import Data.List
    import Test.QuickCheck

    — Primera definición (poco eficiente):
    rotaciones :: Integer -> [Integer]
    rotaciones x = map read [drop n xs ++ take n xs | n [Integer]
    rotaciones’ x = map read (tail (zipWith (++) (tails xs) (inits xs)))
    where xs = show x

    nRotacionesDivisibles :: Integer -> Int
    nRotacionesDivisibles = length . filter (\x -> x mod 4 == 0) . rotaciones’

    — Sabiendo que un número es divisible por 4 si sus dos últimas cifras lo son, se crean las siguientes funciones:
    digitos :: Integer -> [String]
    digitos a = [[x] | x [Int]
    lista n = map read (zipWith (++) (x:xs) (xs++[x]))
    where (x:xs) = digitos n

    nRotacionesDivisibles’ :: Integer -> Int
    nRotacionesDivisibles’ = length . filter (\x -> x mod 4 == 0) . lista

    — Se comprueba con QuickCheck la equivalencia entre definiciones:
    prop :: Integer -> Property
    prop x = x > 0 ==> nRotacionesDivisibles x == nRotacionesDivisibles’ x

    — La comprobación es:
    — *Main> quickCheck prop
    — +++ OK, passed 100 tests.

    — El cálculo del último ejemplo:
    — *Main> nRotacionesDivisibles’ (1234^50000)
    — 38684
    — (0.35 secs, 709,561,984 bytes)

  2. Y ahora en estilo haskell… :D

  3. A la tercera va la vencida…