My Weblog

Blog about programming and math

SPOJ 31. Fast Multiplication

SPOJ 31. Fast Multiplication can be solved using ByteString in Haskell. Just parse the input and rest Haskell will do for you.

import Data.List
import qualified Data.ByteString.Char8 as BS


solve :: [ Integer ] ->  BS.ByteString
solve (a: b: _) = BS.pack.show $ a * b

readInteger :: BS.ByteString -> Integer
readInteger xs = case BS.readInteger xs of 
			Just ( t , _ )  -> t
			_               -> error " something wrong with parsing "
main = BS.interact $ BS.unlines . map ( solve . map readInteger . BS.words ) . tail . BS.lines


Advertisements

May 31, 2011 - Posted by | Programming | ,

1 Comment »

  1. Nice solution. You can get rid of readInteger:

    solve (Just (a,_):Just (b,_):_) = BS.pack.show $ a * b

    Comment by Simon | July 28, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: