Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/Jacobsthal-numbers/Haskell/jacobsthal-numbers-1.hs
Normal file
25
Task/Jacobsthal-numbers/Haskell/jacobsthal-numbers-1.hs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
jacobsthal :: [Integer]
|
||||
jacobsthal = 0 : 1 : zipWith (\x y -> 2 * x + y) jacobsthal (tail jacobsthal)
|
||||
|
||||
jacobsthalLucas :: [Integer]
|
||||
jacobsthalLucas = 2 : 1 : zipWith (\x y -> 2 * x + y) jacobsthalLucas (tail jacobsthalLucas)
|
||||
|
||||
jacobsthalOblong :: [Integer]
|
||||
jacobsthalOblong = zipWith (*) jacobsthal (tail jacobsthal)
|
||||
|
||||
isPrime :: Integer -> Bool
|
||||
isPrime n = n > 1 && not (or [n `mod` i == 0 | i <- [2 .. floor (sqrt (fromInteger n))]])
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
putStrLn "First 30 Jacobsthal numbers:"
|
||||
print $ take 30 jacobsthal
|
||||
putStrLn ""
|
||||
putStrLn "First 30 Jacobsthal-Lucas numbers:"
|
||||
print $ take 30 jacobsthalLucas
|
||||
putStrLn ""
|
||||
putStrLn "First 20 Jacobsthal oblong numbers:"
|
||||
print $ take 20 jacobsthalOblong
|
||||
putStrLn ""
|
||||
putStrLn "First 10 Jacobsthal primes:"
|
||||
print $ take 10 $ filter isPrime jacobsthal
|
||||
50
Task/Jacobsthal-numbers/Haskell/jacobsthal-numbers-2.hs
Normal file
50
Task/Jacobsthal-numbers/Haskell/jacobsthal-numbers-2.hs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import Data.List (intercalate, transpose, uncons, unfoldr)
|
||||
import Data.List.Split (chunksOf)
|
||||
import Data.Numbers.Primes (isPrime)
|
||||
import Text.Printf (printf)
|
||||
|
||||
-------------------- JACOBSTHAL NUMBERS ------------------
|
||||
|
||||
jacobsthal :: [Integer]
|
||||
jacobsthal = jacobsthalish (0, 1)
|
||||
|
||||
jacobsthalish :: (Integer, Integer) -> [Integer]
|
||||
jacobsthalish = unfoldr go
|
||||
where
|
||||
go (a, b) = Just (a, (b, 2 * a + b))
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
(putStrLn . format)
|
||||
[ ( "terms of the Jacobsthal sequence",
|
||||
30,
|
||||
jacobsthal
|
||||
),
|
||||
( "Jacobsthal-Lucas numbers",
|
||||
30,
|
||||
jacobsthalish (2, 1)
|
||||
),
|
||||
( "Jacobsthal oblong numbers",
|
||||
20,
|
||||
zipWith (*) jacobsthal (tail jacobsthal)
|
||||
),
|
||||
( "Jacobsthal primes",
|
||||
10,
|
||||
filter isPrime jacobsthal
|
||||
)
|
||||
]
|
||||
|
||||
format :: (String, Int, [Integer]) -> String
|
||||
format (k, n, xs) =
|
||||
show n <> (' ' : k) <> ":\n"
|
||||
<> table
|
||||
" "
|
||||
(chunksOf 5 $ show <$> take n xs)
|
||||
|
||||
table :: String -> [[String]] -> String
|
||||
table gap rows =
|
||||
let ws = maximum . fmap length <$> transpose rows
|
||||
pw = printf . flip intercalate ["%", "s"] . show
|
||||
in unlines $ intercalate gap . zipWith pw ws <$> rows
|
||||
Loading…
Add table
Add a link
Reference in a new issue