1 {-
2 - Id Example Program
3 - Ensnaffled by SLPJ from MIT via
4 - RPaul <rpaul@juicy-juice.lcs.mit.edu> 93/08/26.
5 - Original author: Steve Heller
6 -}
8 module Main (main) where
9 import Data.Array
10 import System.Environment
11 import Control.Monad (replicateM_)
13 -- Generation of radicals
15 data Radical = H | C Radical Radical Radical
17 three_partitions :: Int -> [(Int,Int,Int)]
18 three_partitions m =
19 [ (i,j,k) | i <- [0..(div m 3)], j <- [i..(div (m-i) 2)], k <- [m - (i+j)]]
21 remainders [] = []
22 remainders (r:rs) = (r:rs) : (remainders rs)
24 radical_generator :: Int -> Array Int [Radical]
25 radical_generator n =
26 radicals
27 where
28 radicals =
29 array (0,n) ((0,[H]) : [(j,rads_of_size_n radicals j) | j <- [1..n]])
31 rads_of_size_n :: Array Int [Radical] -> Int -> [Radical]
32 rads_of_size_n radicals n =
33 [ (C ri rj rk)
34 | (i,j,k) <- (three_partitions (n-1)),
35 (ri:ris) <- (remainders (radicals!i)),
36 (rj:rjs) <- (remainders (if (i==j) then (ri:ris) else radicals!j)),
37 rk <- (if (j==k) then (rj:rjs) else radicals!k)]
39 -- Generation of paraffins.
41 data Paraffin = BCP Radical Radical | CCP Radical Radical Radical Radical
43 bcp_generator :: Array Int [Radical] -> Int -> [Paraffin]
44 bcp_generator radicals n =
45 if (odd n) then []
46 else
47 [ (BCP r1 r2) | (r1:r1s) <- (remainders (radicals!(div n 2))),
48 r2 <- (r1:r1s) ]
50 four_partitions :: Int -> [(Int,Int,Int,Int)]
51 four_partitions m =
52 [ (i,j,k,l)
53 | i <- [0..(div m 4)],
54 j <- [i..(div (m-i) 3)],
55 k <- [(max j (ceiling ((fromIntegral m)/(fromInteger 2)) - i - j))..(div (m-i-j) 2)],
56 l <- [(m - (i+j+k))]]
58 ccp_generator :: Array Int [Radical] -> Int -> [Paraffin]
59 ccp_generator radicals n =
60 [ (CCP ri rj rk rl)
61 | (i,j,k,l) <- (four_partitions (n-1)),
62 (ri:ris) <- (remainders (radicals!i)),
63 (rj:rjs) <- (remainders (if (i==j) then (ri:ris) else radicals!j)),
64 (rk:rks) <- (remainders (if (j==k) then (rj:rjs) else radicals!k)),
65 rl <- (if (k==l) then (rk:rks) else radicals!l)]
67 bcp_until :: Int -> [Int]
68 bcp_until n =
69 [length(bcp_generator radicals j) | j <- [1..n]]
70 where
71 radicals = radical_generator (div n 2)
73 ccp_until :: Int -> [Int]
74 ccp_until n =
75 [length(ccp_generator radicals j) | j <- [1..n]]
76 where
77 radicals = radical_generator (div n 2)
79 paraffins_until :: Int -> [Int]
80 paraffins_until n =
81 [length (bcp_generator radicals j) + length (ccp_generator radicals j)
82 | j <- [1..n]]
83 where
84 radicals = radical_generator (div n 2)
86 main = replicateM_ 1000 \$ do
87 [arg] <- getArgs
88 let num = read arg
89 print [length (rads!i) | rads <- [(radical_generator num)], i <- [0..num]]
90 print (bcp_until num)
91 print (ccp_until num)
92 print (paraffins_until num)