[ create a new paste ] login | about

Link: http://codepad.org/sAbq3Qw0    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Aug 25:
; integer logarithm

(define (leftmost-digit base n)
  (if (< n base)
      n
      (let ((leftmost-pair (leftmost-digit (* base base) n)))
        (if (< leftmost-pair base)
            leftmost-pair
            (quotient leftmost-pair base)))))

(define (leftmost-digit+ base n)
  (if (< n base)
      (values n 0)
      (call-with-values (lambda () (leftmost-digit+ (* base base) n))
        (lambda (leftmost-pair count)
          (if (< leftmost-pair base)
              (values leftmost-pair (* count 2))
              (values (quotient leftmost-pair base) (+ (* count 2) 1)))))))

(display (leftmost-digit 10 816305093398751331727331379663195459013258742431006753294691576)) (newline)

(call-with-values
  (lambda () (leftmost-digit+ 10 46729885))
  (lambda (leftmost-digit discard-count)
    (display leftmost-digit) (newline)
    (display discard-count) (newline)))


Output:
1
2
3
8
4
7


Create a new paste based on this one


Comments: