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:
Simpler, and tail recursive.
(define (leftmost-digit radix n)
(if (> radix n)
n
(leftmost-digit radix (/ n radix))))
Kevin, count the operations. ;^)
...and count them using actual bit complexity, for proper comparison. :)
Post a Comment