Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Padovan-sequence/Haskell/padovan-sequence-1.hs
Normal file
41
Task/Padovan-sequence/Haskell/padovan-sequence-1.hs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
-- list of Padovan numbers using recurrence
|
||||
pRec = map (\(a,_,_) -> a) $ iterate (\(a,b,c) -> (b,c,a+b)) (1,1,1)
|
||||
|
||||
-- list of Padovan numbers using self-referential lazy lists
|
||||
pSelfRef = 1 : 1 : 1 : zipWith (+) pSelfRef (tail pSelfRef)
|
||||
|
||||
-- list of Padovan numbers generated from floor function
|
||||
pFloor = map f [0..]
|
||||
where f n = floor $ p**fromInteger (pred n) / s + 0.5
|
||||
p = 1.324717957244746025960908854
|
||||
s = 1.0453567932525329623
|
||||
|
||||
-- list of L-system strings
|
||||
lSystem = iterate f "A"
|
||||
where f [] = []
|
||||
f ('A':s) = 'B':f s
|
||||
f ('B':s) = 'C':f s
|
||||
f ('C':s) = 'A':'B':f s
|
||||
|
||||
-- check if first N elements match
|
||||
checkN n as bs = take n as == take n bs
|
||||
|
||||
main = do
|
||||
putStr "P_0 .. P_19: "
|
||||
putStrLn $ unwords $ map show $ take 20 pRec
|
||||
|
||||
putStr "The floor- and recurrence-based functions "
|
||||
putStr $ if checkN 64 pRec pFloor then "match" else "do not match"
|
||||
putStr " from P_0 to P_63.\n"
|
||||
|
||||
putStr "The self-referential- and recurrence-based functions "
|
||||
putStr $ if checkN 64 pRec pSelfRef then "match" else "do not match"
|
||||
putStr " from P_0 to P_63.\n\n"
|
||||
|
||||
putStr "The first 10 L-system strings are:\n"
|
||||
putStrLn $ unwords $ take 10 lSystem
|
||||
|
||||
putStr "\nThe floor- and L-system-based functions "
|
||||
putStr $ if checkN 32 pFloor (map length lSystem)
|
||||
then "match" else "do not match"
|
||||
putStr " from P_0 to P_31.\n"
|
||||
57
Task/Padovan-sequence/Haskell/padovan-sequence-2.hs
Normal file
57
Task/Padovan-sequence/Haskell/padovan-sequence-2.hs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import Data.List (unfoldr)
|
||||
|
||||
--------------------- PADOVAN NUMBERS --------------------
|
||||
|
||||
padovans :: [Integer]
|
||||
padovans = unfoldr f (1, 1, 1)
|
||||
where
|
||||
f (a, b, c) = Just (a, (b, c, a + b))
|
||||
|
||||
|
||||
padovanFloor :: [Integer]
|
||||
padovanFloor = unfoldr f 0
|
||||
where
|
||||
f = Just . (((,) . g) <*> succ)
|
||||
|
||||
g = floor . (0.5 +) . (/ s) . (p **) . fromInteger . pred
|
||||
p = 1.324717957244746025960908854
|
||||
s = 1.0453567932525329623
|
||||
|
||||
|
||||
padovanLSystem :: [String]
|
||||
padovanLSystem = unfoldr f "A"
|
||||
where
|
||||
f = Just . ((,) <*> concatMap rule)
|
||||
|
||||
rule 'A' = "B"
|
||||
rule 'B' = "C"
|
||||
rule 'C' = "AB"
|
||||
|
||||
|
||||
-------------------------- TESTS -------------------------
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
putStrLn
|
||||
[ "First 20 padovans:\n",
|
||||
show $ take 20 padovans,
|
||||
[],
|
||||
"The recurrence and floor based functions",
|
||||
"match over 64 terms:\n",
|
||||
show $ prefixesMatch padovans padovanFloor 64,
|
||||
[],
|
||||
"First 10 L-System strings:\n",
|
||||
show $ take 10 padovanLSystem,
|
||||
[],
|
||||
"The length of the first 32 strings produced",
|
||||
"is the Padovan sequence:\n",
|
||||
show $
|
||||
prefixesMatch
|
||||
padovans
|
||||
(fromIntegral . length <$> padovanLSystem)
|
||||
32
|
||||
]
|
||||
|
||||
prefixesMatch :: Eq a => [a] -> [a] -> Int -> Bool
|
||||
prefixesMatch xs ys n = and (zipWith (==) (take n xs) ys)
|
||||
Loading…
Add table
Add a link
Reference in a new issue