-- -*- haskell -*-
-- Time-stamp: <Thu Nov 23 2000 22:32:02 Stardate: [-30]5774.69 hwloidl>
--
-- Perform a factorial-like computation, actually just sum, using a 
-- parallel divide-and-conquer paradigm, with a threshhold
-- Haskell98 version.
-----------------------------------------------------------------------------

module Main(main) where

import System(getArgs)
import IO
import Parallel

main = do args <- getArgs
          let 
            n = read (args!!0)  -- size of the interval
            t = read (args!!1)  -- threshold
            res = pfact n t
          hPutStrLn stderr ("pfact of " ++ (show n) ++ 
                    " with threshhold " ++ (show t) ++ 
                    " = " ++ (show res)) 

pfact :: Integer -> Integer -> Integer
pfact n t = pfact' 1 n t

-- thresholding version
pfact' :: Integer -> Integer -> Integer -> Integer
pfact' m n t | (n-m) <= t = sum [m..n]             -- seq version below t
             | otherwise  = left `par` right `seq` -- par d&c version
                            (left + right)
                            where mid = (m + n) `div` 2
                                  left = pfact' m mid t 
                                  right = pfact' (mid+1) n t
















