Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-29 Thread Daniel Peebles
Although this isn't a very "general approach", I just submitted a patch to GHC (not yet merged) with a gmp binding to mpz_sizeinbase, which would allow for very quick computation of number of digits in any base. On Sat, Aug 29, 2009 at 9:12 PM, Edward Kmett wrote: > I have a version of this inside

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-29 Thread Edward Kmett
I have a version of this inside of the monoid library buried in the Data.Ring.Semi.BitSet module: http://comonad.com/haskell/monoids/dist/doc/html/monoids/src/Data-Ring-Semi-BitSet.html#hwm To do any bette

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-29 Thread Bertram Felgenhauer
Uwe Hollerbach wrote: > Here's my version... maybe not as elegant as some, but it seems to > work. For base 2 (or 2^k), it's probably possible to make this even > more efficient by just walking along the integer as stored in memory, > but that difference probably won't show up until at least tens o

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-26 Thread Uwe Hollerbach
Here's my version... maybe not as elegant as some, but it seems to work. For base 2 (or 2^k), it's probably possible to make this even more efficient by just walking along the integer as stored in memory, but that difference probably won't show up until at least tens of thousands of digits. Uwe i

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-26 Thread George Pollard
2009/8/26 Lennart Augustsson : > Try 10^1000 Cheat :) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-26 Thread Daniel Fischer
Am Mittwoch 26 August 2009 06:29:47 schrieb George Pollard: > You could also fudge the input: > > {-# LANGUAGE NoMonomorphismRestriction #-} > > log10 = floor . logBase 10 . (0.5+) . fromIntegral > > numDigits n | n < 0 = 1 + numDigits (-n) > numDigits 0 = 1 > numDigits n = 1 + log1

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-25 Thread George Pollard
You could also fudge the input: {-# LANGUAGE NoMonomorphismRestriction #-} log10 = floor . logBase 10 . (0.5+) . fromIntegral numDigits n | n < 0 = 1 + numDigits (-n) numDigits 0 = 1 numDigits n = 1 + log10 n -- checked [0..10^8], finding a counter-example is left as an exercise

Re[2]: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-25 Thread Bulat Ziganshin
Hello Henning, Tuesday, August 25, 2009, 6:11:00 PM, you wrote: >> digits = iterate (`div` 10) >>> takeWhile (>0) >>> length > This needs quadratic time with respect to the number of digits, doesn't > it? why? i think that `show` uses pretty the same way to build list of digits, so we just omi

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-25 Thread Henning Thielemann
On Sat, 22 Aug 2009, Bulat Ziganshin wrote: 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 This needs quadratic time with respect

Re[2]: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-22 Thread Bulat Ziganshin
Hello José, Saturday, August 22, 2009, 10:36:33 PM, you wrote: ghci doesn't optimize > It looks like length . show is faster -- Best regards, Bulatmailto:bulat.zigans...@gmail.com ___ Haskell-Cafe mailing list Haskell-C

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-22 Thread José Prous
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^100 Prelude Control.Arrow> :set +s Prelude Control.Arrow> numDigits n 301030 (0.39

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-22 Thread Bulat Ziganshin
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, Bulatmailto:bulat.zigans...@gmail.com

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-22 Thread Eugene Kirpichov
Ouch, my bad. length.show is better :) 2009/8/22 Derek Elkins : > 2009/8/22 Eugene Kirpichov : >> Use 'round' instead of 'truncate'. >> >> Prelude> let numDigits = (+1) . round . logBase 10 . fromIntegral >> Prelude> map (numDigits . (10^)) [0..9] >> [1,2,3,4,5,6,7,8,9,10] >> > > round won't work

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-22 Thread Derek Elkins
On Sat, Aug 22, 2009 at 12:31 PM, Derek Elkins wrote: > 2009/8/22 Eugene Kirpichov : >> Use 'round' instead of 'truncate'. >> >> Prelude> let numDigits = (+1) . round . logBase 10 . fromIntegral >> Prelude> map (numDigits . (10^)) [0..9] >> [1,2,3,4,5,6,7,8,9,10] >> > > round won't work because 999

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-22 Thread Derek Elkins
2009/8/22 Eugene Kirpichov : > Use 'round' instead of 'truncate'. > > Prelude> let numDigits = (+1) . round . logBase 10 . fromIntegral > Prelude> map (numDigits . (10^)) [0..9] > [1,2,3,4,5,6,7,8,9,10] > round won't work because 999 is close to 1000. You simply need to use logBase 10 as a guess

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-22 Thread Eugene Kirpichov
Or better numDigits = length . show It's probably even faster. 2009/8/22 Eugene Kirpichov : > Use 'round' instead of 'truncate'. > > Prelude> let numDigits = (+1) . round . logBase 10 . fromIntegral > Prelude> map (numDigits . (10^)) [0..9] > [1,2,3,4,5,6,7,8,9,10] > > 2009/8/22 Roberto López : >>

Re: [Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-22 Thread Eugene Kirpichov
Use 'round' instead of 'truncate'. Prelude> let numDigits = (+1) . round . logBase 10 . fromIntegral Prelude> map (numDigits . (10^)) [0..9] [1,2,3,4,5,6,7,8,9,10] 2009/8/22 Roberto López : > Ok. I wonder if someone could help me with this problem... > > I want to calculate the number of digits o

[Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

2009-08-22 Thread Roberto López
Ok. I wonder if someone could help me with this problem... I want to calculate the number of digits of a positive integer. I was thinking of ... numDigits n = truncate (logBase 10 n) + 1 But (logBase 10 1000) = 2.9996 so numDigits 1000 = 2. Maybe adding a small amount