Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
16
Task/Recamans-sequence/Haskell/recamans-sequence-1.hs
Normal file
16
Task/Recamans-sequence/Haskell/recamans-sequence-1.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
recaman :: Int -> [Int]
|
||||
recaman n = fst <$> reverse (go n)
|
||||
where
|
||||
go 0 = []
|
||||
go 1 = [(0, 1)]
|
||||
go x =
|
||||
let xs@((r, i):_) = go (pred x)
|
||||
back = r - i
|
||||
in ( if 0 < back && not (any ((back ==) . fst) xs)
|
||||
then back
|
||||
else r + i
|
||||
, succ i) :
|
||||
xs
|
||||
|
||||
main :: IO ()
|
||||
main = print $ recaman 15
|
||||
42
Task/Recamans-sequence/Haskell/recamans-sequence-2.hs
Normal file
42
Task/Recamans-sequence/Haskell/recamans-sequence-2.hs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import Data.Set (Set, fromList, insert, isSubsetOf, member, size)
|
||||
import Data.Bool (bool)
|
||||
|
||||
firstNRecamans :: Int -> [Int]
|
||||
firstNRecamans n = reverse $ recamanUpto (\(_, i, _) -> n == i)
|
||||
|
||||
firstDuplicateR :: Int
|
||||
firstDuplicateR = head $ recamanUpto (\(rs, _, set) -> size set /= length rs)
|
||||
|
||||
recamanSuperset :: Set Int -> [Int]
|
||||
recamanSuperset setInts =
|
||||
tail $ recamanUpto (\(_, _, setR) -> isSubsetOf setInts setR)
|
||||
|
||||
recamanUpto :: (([Int], Int, Set Int) -> Bool) -> [Int]
|
||||
recamanUpto p = rs
|
||||
where
|
||||
(rs, _, _) =
|
||||
until
|
||||
p
|
||||
(\(rs@(r:_), i, seen) ->
|
||||
let n = nextR seen i r
|
||||
in (n : rs, succ i, insert n seen))
|
||||
([0], 1, fromList [0])
|
||||
|
||||
nextR :: Set Int -> Int -> Int -> Int
|
||||
nextR seen i r =
|
||||
let back = r - i
|
||||
in bool back (r + i) (0 > back || member back seen)
|
||||
|
||||
-- TEST ---------------------------------------------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
(putStrLn . unlines)
|
||||
[ "First 15 Recamans:"
|
||||
, show $ firstNRecamans 15
|
||||
, []
|
||||
, "First duplicated Recaman:"
|
||||
, show firstDuplicateR
|
||||
, []
|
||||
, "Length of Recaman series required to include [0..1000]:"
|
||||
, (show . length . recamanSuperset) $ fromList [0 .. 1000]
|
||||
]
|
||||
47
Task/Recamans-sequence/Haskell/recamans-sequence-3.hs
Normal file
47
Task/Recamans-sequence/Haskell/recamans-sequence-3.hs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import Data.List (find, findIndex, nub)
|
||||
import Data.Maybe (fromJust)
|
||||
import Data.Set (Set, fromList, insert, isSubsetOf, member)
|
||||
|
||||
--- INFINITE STREAM OF RECAMAN SERIES OF GROWING LENGTH --
|
||||
rSeries :: [[Int]]
|
||||
rSeries =
|
||||
scanl
|
||||
( \rs@(r : _) i ->
|
||||
let back = r - i
|
||||
nxt
|
||||
| 0 > back || elem back rs = r + i
|
||||
| otherwise = back
|
||||
in nxt : rs
|
||||
)
|
||||
[0]
|
||||
[1 ..]
|
||||
|
||||
----------- INFINITE STREAM OF RECAMAN-GENERATED ---------
|
||||
--------------- INTEGER SETS OF GROWING SIZE -------------
|
||||
rSets :: [(Set Int, Int)]
|
||||
rSets =
|
||||
scanl
|
||||
( \(seen, r) i ->
|
||||
let back = r - i
|
||||
nxt
|
||||
| 0 > back || member back seen = r + i
|
||||
| otherwise = back
|
||||
in (insert nxt seen, nxt)
|
||||
)
|
||||
(fromList [0], 0)
|
||||
[1 ..]
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main = do
|
||||
let setK = fromList [0 .. 1000]
|
||||
(putStrLn . unlines)
|
||||
[ "First 15 Recamans:",
|
||||
show . reverse . fromJust $ find ((15 ==) . length) rSeries,
|
||||
[],
|
||||
"First duplicated Recaman:",
|
||||
show . head . fromJust $ find ((/=) <$> length <*> (length . nub)) rSeries,
|
||||
[],
|
||||
"Length of Recaman series required to include [0..1000]:",
|
||||
show . fromJust $ findIndex (\(setR, _) -> isSubsetOf setK setR) rSets
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue