Integer long division: Difference between revisions

Content added Content deleted
Line 17: Line 17:
; ($/ 1 17) => 588235294117647 ; 16
; ($/ 1 17) => 588235294117647 ; 16
(assert (and (integerp a) (integerp b) (not (zerop b))))
(assert (and (integerp a) (integerp b) (not (zerop b))))
(do* (c
(do* (c (i0 (1+ (max (factor-multiplicity b 2) (factor-multiplicity b 5)))) ; the position which marks the beginning of the period
(i0 (1+ (max (factor-multiplicity b 2) (factor-multiplicity b 5)))) ; the position which marks the beginning of the period
(r a (* 10 r)) ; remainder
(i 0 (1+ i)) ; iterations counter
(r a (* 10 r)) ; remainder
(i 0 (1+ i)) ; iterations counter
(rem (if (= i i0) r -1) (if (= i i0) r rem)) ) ; the first remainder against which to check for repeating remainders
((and (= r rem) (not (= i i0))) (- i i0))
(rem (if (= i i0) r -1) (if (= i i0) r rem)) ) ; the first remainder against which to check for repeating remainders
((and (= r rem) (not (= i i0))) (- i i0))
(multiple-value-setq (c r) (floor r b))
(multiple-value-setq (c r) (floor r b))
(princ c) ))
(princ c) ))
Line 30: Line 31:
; (factor-multiplicity 12 2) => 2
; (factor-multiplicity 12 2) => 2
(do* ((i 0 (1+ i))
(do* ((i 0 (1+ i))
(n (/ n factor) (/ n factor)) )
(n (/ n factor) (/ n factor)) )
((not (integerp n)) i)
((not (integerp n)) i)
() ))
() ))


</lang>
</lang>