[Haskell-beginners] This code does not work : conversion error

Graham Gill math.simplex at gmail.com
Sun Dec 1 14:16:58 UTC 2013


Once you've made those changes, you also need

n `mod` 2^k

instead of 2^k `mod` n.

Finally, using e `div` f, and round() in the definitions of e and f, to 
give you the largest power of 2 dividing n doesn't work correctly. For 
example:
maxexp2 4 = 1 (should be 2)
maxexp2 16 = 3 (should be 4)
maxexp2 18 = 0 (should be 1)
maxexp2 28 = 0 (should be 2)

But that's not a Haskell error, that's a problem with the algorithm.

(Sorry if I've misinterpreted what you're trying to do.)

Graham

On 01/12/2013 3:17 AM, mukesh tiwari wrote:
> Hi Willie,
> Here is the code  modified
> maxexp2:: Int -> Int
> maxexp2 n
>     |n== 0 || 2^k `mod` n /=0 =0
>     |otherwise = k
>           where
>             k = e `div` f
>             e  = round  ( log ( fromIntegral n ) )
>             f = round ( (log 2.0 ))
>
> When you computing  k then use backticks (`) [1]  not the single quote 
> ( ' )
>
> [1] http://book.realworldhaskell.org/read/functional-programming.html( 
> <http://book.realworldhaskell.org/read/functional-programming.html%28> 
> See Infix function )
>
>
> On Sun, Dec 1, 2013 at 12:52 PM, willie ekaputra 
> <willieekaputra at gmail.com <mailto:willieekaputra at gmail.com>> wrote:
>
>     Hi everyone !
>     I am newbie and I made this code for counting k, so that 2^k
>     divisor of n.Somehow it doesn't work.
>     Anyone knows what is  wrong?
>
>     Regards and thanks.
>     Wili.
>
>     maxexp2:: Int -> Int
>     maxexp2 n
>         |n== 0 || 2^k 'mod' n /=0 =0
>         |otherwise = k
>               Where
>                k= e ' div' f
>                e=round (fromIntegral (log n))
>                 f = round (fromIntegral (log 2))
>
>
>     _______________________________________________
>     Beginners mailing list
>     Beginners at haskell.org <mailto:Beginners at haskell.org>
>     http://www.haskell.org/mailman/listinfo/beginners
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20131201/0042b947/attachment.html>


More information about the Beginners mailing list