Files
  • main.hs
main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import Data.Foldable (for_)

-- | 階乗
fact :: Int -> Integer
fact 0 = 1
fact n = fromIntegral n * fact (n-1)


-- | 組み合わせの数
combination :: Int -> Int -> Integer
n `combination` k = fact n `div` (fact (n-k) * fact k)


-- | パスカルの三角形
pascal :: [Integer]
pascal = [n `combination` k | n <- [0..], k <- [0..n]]


-- | 三角数
tri :: Int -> Int
tri n = n * (n+1) `div` 2


-- | ある数がパスカルの三角形に出現する回数を数える関数
countInPascal :: Int -> Int
countInPascal a = length . filter (== fromIntegral a) $ take (tri (a+1)) pascal


main :: IO ()
main = for_ [2..3003] $ \a ->
  let n = countInPascal a
   in putStrLn $ "N(" ++ show a ++ ") = " ++ show n
GHCi, version 8.6.3