[Haskell-cafe] How to calculate de number of digits of an
integer? (was: Is logBase right?)
José Prous
hiena03 at gmail.com
Sat Aug 22 14:36:33 EDT 2009
It looks like length . show is faster
Prelude Control.Arrow> let numDigits n = length $ show n
Prelude Control.Arrow> let digits = iterate (`div` 10) >>> takeWhile (>0)
>>> length
Prelude Control.Arrow> let n=2^1000000
Prelude Control.Arrow> :set +s
Prelude Control.Arrow> numDigits n
301030
(0.39 secs, 23001616 bytes)
Prelude Control.Arrow> digits n
301030
(51.06 secs, 19635437248 bytes)
2009/8/22 Bulat Ziganshin <bulat.ziganshin at gmail.com>
> Hello Roberto,
>
> Saturday, August 22, 2009, 9:19:26 PM, you wrote:
>
> > I want to calculate the number of digits of a positive integer. I was
>
> fastest way
>
> digits = iterate (`div` 10) >>> takeWhile (>0) >>> length
>
>
> --
> Best regards,
> Bulat mailto:Bulat.Ziganshin at gmail.com
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090822/3095f192/attachment.html
More information about the Haskell-Cafe
mailing list