On 2023-05-22, James Cloos <
cl...@jhcloos.com> wrote:
> given (declare (integer dividend divisor)), this works:
>
> (do (( n dividend (/ n divisor))) ((typep n 'ratio) (* divisor n)))
>
> but is there a way to return n /before/ the division which return a ratio?
One possibility: keep dividing until we get something with a denominator
that isn't 1:
(defun strip-factor (integer factor)
(do ((prev integer next)
(next integer (/ prev factor)))
((not (eql 1 (denominator next))) prev)))
Another one: we use the truncate function, which returns two values:
quotient and remainder. We stop when we get a nonzero remainder, and
use the previous value:
(defun strip-factor (integer factor)
(loop with quotient and remainder
do (setf (values quotient remainder) (truncate integer factor))
until (not (zerop remainder))
do (setf integer quotient)
finally (return integer)))
--
TXR Programming Language:
http://nongnu.org/txr
Cygnal: Cygwin Native Application Library:
http://kylheku.com/cygnal
Mastodon: @
Kazi...@mstdn.ca