# [plt-scheme] Integer-length

Joe Marshall wrote:
> On 3/22/06, Jens Axel Søgaard <jensaxel at soegaard.net> wrote:
>
>>Is there a faster way of doing this?
>
> Yes.
> (define (integer-length n)
> (define (iter i next length nextlength)
> (if (> next n)
> (if (= i 1)
> 1
> (+ length (integer-length (floor->exact (/ n i)))))
> (iter next (* next next) nextlength
> (+ nextlength nextlength))))
> (iter 1 2 1 1))
Nice.
> As an example, (integer-length (expt 2 50000)) requires only 62
> multiplications and 6 divisions.
>
> If you are only interested in numbers with a few bits, however, a
> nested conditional will most likely be the fastest.
In my current context I am interested in numbers below 100000,
but I'll remember the technique.
--
Jens Axel Søgaard