Monday, March 18, 2013

About nothing

Here's a way to quickly compute the leftmost digit of a number:

(define (leftmost-digit radix n)
  (if (> radix n)
      n
      (let ((leftmost-digit-pair (leftmost-digit (* radix radix) n)))
 (if (> radix leftmost-digit-pair)
     leftmost-digit-pair
     (quotient leftmost-digit-pair radix)))))

It's harder to compute the remaining digits.

3 comments:

kevin brintnall said...

Simpler, and tail recursive.

(define (leftmost-digit radix n)
(if (> radix n)
n
(leftmost-digit radix (/ n radix))))

Pierpaolo Bernardi said...

Kevin, count the operations. ;^)

Gath-Gealaich said...

...and count them using actual bit complexity, for proper comparison. :)