Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/Square-but-not-cube/Haskell/square-but-not-cube-1.hs
Normal file
26
Task/Square-but-not-cube/Haskell/square-but-not-cube-1.hs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
{-# LANGUAGE TupleSections #-}
|
||||
|
||||
import Control.Monad (join)
|
||||
import Data.List (partition, sortOn)
|
||||
import Data.Ord (comparing)
|
||||
|
||||
|
||||
------------------- SQUARE BUT NOT CUBE ------------------
|
||||
|
||||
isCube :: Int -> Bool
|
||||
isCube n = n == round (fromIntegral n ** (1 / 3)) ^ 3
|
||||
|
||||
both, only :: [Int]
|
||||
(both, only) = partition isCube $ join (*) <$> [1 ..]
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
(putStrLn . unlines) $
|
||||
uncurry ((<>) . show)
|
||||
<$> sortOn
|
||||
fst
|
||||
( ((," (also cube)") <$> take 3 both)
|
||||
<> ((,"") <$> take 30 only)
|
||||
)
|
||||
21
Task/Square-but-not-cube/Haskell/square-but-not-cube-2.hs
Normal file
21
Task/Square-but-not-cube/Haskell/square-but-not-cube-2.hs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import Control.Monad (join)
|
||||
|
||||
------------------- SQUARE BUT NOT CUBE ------------------
|
||||
|
||||
cubeRoot :: Int -> Int
|
||||
cubeRoot = round . (** (1 / 3)) . fromIntegral
|
||||
|
||||
isCube :: Int -> Bool
|
||||
isCube = (==) <*> ((^ 3) . cubeRoot)
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
(putStrLn . unlines) $
|
||||
((<>) . show <*> cubeNote)
|
||||
<$> take 33 (join (*) <$> [1 ..])
|
||||
|
||||
cubeNote :: Int -> String
|
||||
cubeNote x
|
||||
| isCube x = " (also cube of " <> show (cubeRoot x) <> ")"
|
||||
| otherwise = []
|
||||
18
Task/Square-but-not-cube/Haskell/square-but-not-cube-3.hs
Normal file
18
Task/Square-but-not-cube/Haskell/square-but-not-cube-3.hs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
isCube :: Int -> Bool
|
||||
isCube =
|
||||
(==)
|
||||
<*> ((^ 3) . round . (** (1 / 3)) . fromIntegral)
|
||||
|
||||
squares :: Int -> Int -> [Int]
|
||||
squares m n = (>>= id) (*) <$> [m .. n]
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
(putStrLn . unlines) $
|
||||
(<>) . show <*> label <$> squares 1 33
|
||||
|
||||
label :: Int -> String
|
||||
label n
|
||||
| isCube n = " (also cube)"
|
||||
| otherwise = ""
|
||||
Loading…
Add table
Add a link
Reference in a new issue