From: Jeremy Smith
Subject: Questions on the ratio type in Common Lisp and irreducible fractions
Date: 
Message-ID: <X_KQf.1165$Dg5.278@newsfe4-gui.ntli.net>
I've checked the hyperspec, but it doesn't have much to say about ratios.

A ratio is a fraction represented like "1/2" which is a half. You type in (/
1 2) in the Repl and it gives you "1/2".

The first question is, how do I extract the integers on the top and bottom
of the ratio, to the integers 1 and 2?

The second question is, how do I check if the ratio (or fraction) is
irreducible? If I type "(/ 4 16)" it gives me "1/4", not "4/16", because
it's reducible. But if I type "(/ 31 7)", it gives me "31/7" because it's
not reducible.

I can print the result to a string (eg, (format "~a" 31/7)) and compare it
to (format "~d/~d" 31 7) using the string equal function, but that's
probably inefficient. So how can I do this comparison without using
strings?

equal and = both fail to tell the difference between an irreducible fraction
and a reducible one, so I'm stuck unless I use strings.

Thanks for any help,

Jeremy.

From: Eric Lavigne
Subject: Re: Questions on the ratio type in Common Lisp and irreducible fractions
Date: 
Message-ID: <1142128394.464589.53040@z34g2000cwc.googlegroups.com>
Jeremy Smith wrote:
> I've checked the hyperspec, but it doesn't have much to say about ratios.

I checked Paul Graham's Ansi Common Lisp. I expect the Hyperspec also
covers it, but I find Graham's book a bit easier to search.

> A ratio is a fraction represented like "1/2" which is a half. You type in (/
> 1 2) in the Repl and it gives you "1/2".
>
> The first question is, how do I extract the integers on the top and bottom
> of the ratio, to the integers 1 and 2?

(numerator (/ 1 2)) --> 1
(denominator (/ 1 2)) --> 2

> The second question is, how do I check if the ratio (or fraction) is
> irreducible? If I type "(/ 4 16)" it gives me "1/4", not "4/16", because
> it's reducible. But if I type "(/ 31 7)", it gives me "31/7" because it's
> not reducible.

Using the above functions, it's fairly easy.
(defun irreducible-p (numerator denominator)
    (eql
        numerator 
        (numerator (/ numerator denominator))))
From: Eric Lavigne
Subject: Re: Questions on the ratio type in Common Lisp and irreducible fractions
Date: 
Message-ID: <1142128719.336944.52320@u72g2000cwu.googlegroups.com>
> (defun irreducible-p (numerator denominator)
>     (eql
>         numerator
>         (numerator (/ numerator denominator))))

In case the above is confusing, here's what it looks like with
one-letter arguments:

(defun irreducible-p (a b)
    (eql
        a
        (numerator (/ a b))))

Numerator and denominator would ordinarily be good names for those
arguments, but it's confusing in this case since numerator is also a
function.

(irreducible-p 4 16) --> NIL
(irreducible-p 1 4) --> T
From: Jeremy Smith
Subject: Re: Questions on the ratio type in Common Lisp and irreducible fractions
Date: 
Message-ID: <PoLQf.205$qH2.117@newsfe4-win.ntli.net>
Eric Lavigne wrote:

> 
> Jeremy Smith wrote:
>> I've checked the hyperspec, but it doesn't have much to say about ratios.
> 
> I checked Paul Graham's Ansi Common Lisp. I expect the Hyperspec also
> covers it, but I find Graham's book a bit easier to search.

Thanks for the quick response.

I don't have a copy of Ansi Common Lisp unfortunately. I see it's not as
pricey as On Lisp (probably because it's still in print), so I'll get one.

>> A ratio is a fraction represented like "1/2" which is a half. You type in
>> (/ 1 2) in the Repl and it gives you "1/2".
>>
>> The first question is, how do I extract the integers on the top and
>> bottom of the ratio, to the integers 1 and 2?
> 
> (numerator (/ 1 2)) --> 1
> (denominator (/ 1 2)) --> 2

That's easy enough but not easy terms to apropos for!

>> The second question is, how do I check if the ratio (or fraction) is
>> irreducible? If I type "(/ 4 16)" it gives me "1/4", not "4/16", because
>> it's reducible. But if I type "(/ 31 7)", it gives me "31/7" because it's
>> not reducible.
> 
> Using the above functions, it's fairly easy.
> (defun irreducible-p (numerator denominator)
>     (eql
>         numerator
>         (numerator (/ numerator denominator))))

That works fine (and the abbreviated version), the program is a lot faster
now, and I know a little more about ratios.

Thanks,

Jeremy.
From: Bruce Hoult
Subject: Re: Questions on the ratio type in Common Lisp and irreducible fractions
Date: 
Message-ID: <bruce-7DA192.15354412032006@news.clear.net.nz>
In article <·················@newsfe4-win.ntli.net>,
 Jeremy Smith <············@decompiler.org> wrote:

> Eric Lavigne wrote:
> 
> > 
> > Jeremy Smith wrote:
> >> I've checked the hyperspec, but it doesn't have much to say about ratios.
> > 
> > I checked Paul Graham's Ansi Common Lisp. I expect the Hyperspec also
> > covers it, but I find Graham's book a bit easier to search.
> 
> Thanks for the quick response.
> 
> I don't have a copy of Ansi Common Lisp unfortunately. I see it's not as
> pricey as On Lisp (probably because it's still in print), so I'll get one.
> 
> >> A ratio is a fraction represented like "1/2" which is a half. You type in
> >> (/ 1 2) in the Repl and it gives you "1/2".
> >>
> >> The first question is, how do I extract the integers on the top and
> >> bottom of the ratio, to the integers 1 and 2?
> > 
> > (numerator (/ 1 2)) --> 1
> > (denominator (/ 1 2)) --> 2
> 
> That's easy enough but not easy terms to apropos for!

Well, those are the mathematical terms taught in primary school!


> >> The second question is, how do I check if the ratio (or fraction) is
> >> irreducible? If I type "(/ 4 16)" it gives me "1/4", not "4/16", because
> >> it's reducible. But if I type "(/ 31 7)", it gives me "31/7" because it's
> >> not reducible.
> > 
> > Using the above functions, it's fairly easy.
> > (defun irreducible-p (numerator denominator)
> >     (eql
> >         numerator
> >         (numerator (/ numerator denominator))))
> 
> That works fine (and the abbreviated version), the program is a lot faster
> now, and I know a little more about ratios.

This should be a little faster:

(defun irreducible-p (numerator denominator) 
  (eql (gcd numerator denominator) 1))

-- 
Bruce |  41.1670S | \  spoken |          -+-
Hoult | 174.8263E | /\ here.  | ----------O----------
From: William Bland
Subject: Re: Questions on the ratio type in Common Lisp and irreducible fractions
Date: 
Message-ID: <pan.2006.03.15.17.00.25.957820@gmail.com>
On Sun, 12 Mar 2006 02:08:47 +0000, Jeremy Smith wrote:

>> (numerator (/ 1 2)) --> 1
>> (denominator (/ 1 2)) --> 2
> 
> That's easy enough but not easy terms to apropos for!

A full-text search for "rational" on LispDoc does a reasonable job:

http://lispdoc.com/?q=rational&search=Full+text+search

I'm always open to suggestions for how to improve it though.

Best wishes,
	Bill.
From: Richard Fateman
Subject: Re: Questions on the ratio type in Common Lisp and irreducible fractions
Date: 
Message-ID: <5ULQf.38006$_S7.35714@newssvr14.news.prodigy.com>
You've gotten some answers, but they aren't quite right, I think.

If you want to know if two numbers are relatively prime, I suggest
you just use gcd.

If you want a ratio like 1/2  you can type it in exactly that way.
(/ 1 2)  DIVIDES 1 by 2 and produces a ratio object 1/2.

(/ 2 4) similarly divides 2 by 4 and produces a ratio object 1/2.

All ratio objects are in lowest terms, and so  if r is a ratio,
 (gcd (numerator r)(denominator r)) is 1.



RJF 
From: Jeremy Smith
Subject: Re: Questions on the ratio type in Common Lisp and irreducible fractions
Date: 
Message-ID: <61mRf.1429$j7.131@newsfe3-win.ntli.net>
Richard Fateman wrote:

> You've gotten some answers, but they aren't quite right, I think.

They seem to work okay so far.

> If you want to know if two numbers are relatively prime, I suggest
> you just use gcd.

Thanks, I'll look it up.

> If you want a ratio like 1/2  you can type it in exactly that way.

I knew this but I was just recapping.

> All ratio objects are in lowest terms, and so  if r is a ratio,
>  (gcd (numerator r)(denominator r)) is 1.

This works okay.

Thanks!

Jeremy.