Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Linux: the choice of a GNU generation -- ksh@cis.ufl.edu put this on Tshirts in '93


devel / comp.lang.lisp / repeated division returning an integer?

SubjectAuthor
* repeated division returning an integer?James Cloos
+* Re: repeated division returning an integer?Tom Russ
|`* Re: repeated division returning an integer?Tom Russ
| `- Re: repeated division returning an integer?Alan Bawden
+- Re: repeated division returning an integer?Kaz Kylheku
`- Re: repeated division returning an integer?James Cloos

1
repeated division returning an integer?

<m38rdh5e91.fsf@carbon.jhcloos.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17398&group=comp.lang.lisp#17398

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: cloos@jhcloos.com (James Cloos)
Newsgroups: comp.lang.lisp
Subject: repeated division returning an integer?
Date: Sun, 21 May 2023 21:47:06 -0400
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <m38rdh5e91.fsf@carbon.jhcloos.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="986d4edef974b76342129aaae8dde7dc";
logging-data="2100974"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18IoJiuAXRDaHP9/1SDlp3j"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:3XHEavjv0y6fvwsvaX8LBAuXWFc=
sha1:Fr63u5/xnPSTeDcs6kUy4r0lWIE=
Copyright: Copyright 2023 James Cloos
Face: iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAACVBMVEX///8ZGXBQKKnCrDQ3
AAAAJElEQVQImWNgQAAXzwQg4SKASgAlXIEEiwsSIYBEcLaAtMEAADJnB+kKcKioAAAAAElFTkSu
QmCC
OpenPGP: 0x997A9F17ED7DAEA6; url=https://jhcloos.com/public_key/0x997A9F17ED7DAEA6.asc
OpenPGP-Fingerprint: E9E9 F828 61A4 6EA9 0F2B 63E7 997A 9F17 ED7D AEA6
 by: James Cloos - Mon, 22 May 2023 01:47 UTC

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?

-JimC
--
James Cloos <cloos@jhcloos.com> OpenPGP: 0x997A9F17ED7DAEA6

Re: repeated division returning an integer?

<9728cd6d-33a5-421a-a20c-cfe02a26ff0cn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17399&group=comp.lang.lisp#17399

  copy link   Newsgroups: comp.lang.lisp
X-Received: by 2002:ac8:1486:0:b0:3f4:d3a7:9815 with SMTP id l6-20020ac81486000000b003f4d3a79815mr2509655qtj.5.1684779371305;
Mon, 22 May 2023 11:16:11 -0700 (PDT)
X-Received: by 2002:ad4:5591:0:b0:61b:5a20:23e5 with SMTP id
f17-20020ad45591000000b0061b5a2023e5mr2084053qvx.8.1684779370963; Mon, 22 May
2023 11:16:10 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!3.us.feeder.erje.net!feeder.erje.net!border-1.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.lisp
Date: Mon, 22 May 2023 11:16:10 -0700 (PDT)
In-Reply-To: <m38rdh5e91.fsf@carbon.jhcloos.org>
Injection-Info: google-groups.googlegroups.com; posting-host=2620:0:102f:1005:502f:ee94:ca48:b9e1;
posting-account=05zmAwoAAAAJZM-3jv1hCWLHGZQceqwA
NNTP-Posting-Host: 2620:0:102f:1005:502f:ee94:ca48:b9e1
References: <m38rdh5e91.fsf@carbon.jhcloos.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9728cd6d-33a5-421a-a20c-cfe02a26ff0cn@googlegroups.com>
Subject: Re: repeated division returning an integer?
From: taruss@google.com (Tom Russ)
Injection-Date: Mon, 22 May 2023 18:16:11 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 13
 by: Tom Russ - Mon, 22 May 2023 18:16 UTC

On Sunday, May 21, 2023 at 6:48:56 PM UTC-7, James Cloos wrote:
> given (declare (integer dividend divisor)), this works:
>
> (do (( n dividend (/ n divisor))) ((typep n 'ratio) (* divisor n)))

How about

(do ((n dividend (/ n divisor))) ((not (zerop (rem n divisor))) n))

Just make sure you don't pass 0 as either of the inputs....
There may also be a more direct way to compute this, but I can't think of one at the moment.

Re: repeated division returning an integer?

<70d44559-f878-474a-9ab9-94096ae0c2b4n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17400&group=comp.lang.lisp#17400

  copy link   Newsgroups: comp.lang.lisp
X-Received: by 2002:ad4:4e32:0:b0:623:8359:2085 with SMTP id dm18-20020ad44e32000000b0062383592085mr1881153qvb.0.1684779643517;
Mon, 22 May 2023 11:20:43 -0700 (PDT)
X-Received: by 2002:a05:620a:4413:b0:759:5803:3481 with SMTP id
v19-20020a05620a441300b0075958033481mr3182000qkp.9.1684779643218; Mon, 22 May
2023 11:20:43 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.lisp
Date: Mon, 22 May 2023 11:20:43 -0700 (PDT)
In-Reply-To: <9728cd6d-33a5-421a-a20c-cfe02a26ff0cn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2620:0:102f:1005:502f:ee94:ca48:b9e1;
posting-account=05zmAwoAAAAJZM-3jv1hCWLHGZQceqwA
NNTP-Posting-Host: 2620:0:102f:1005:502f:ee94:ca48:b9e1
References: <m38rdh5e91.fsf@carbon.jhcloos.org> <9728cd6d-33a5-421a-a20c-cfe02a26ff0cn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <70d44559-f878-474a-9ab9-94096ae0c2b4n@googlegroups.com>
Subject: Re: repeated division returning an integer?
From: taruss@google.com (Tom Russ)
Injection-Date: Mon, 22 May 2023 18:20:43 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2303
 by: Tom Russ - Mon, 22 May 2023 18:20 UTC

On Monday, May 22, 2023 at 11:16:13 AM UTC-7, Tom Russ wrote:
> On Sunday, May 21, 2023 at 6:48:56 PM UTC-7, James Cloos wrote:
> > given (declare (integer dividend divisor)), this works:
> >
> > (do (( n dividend (/ n divisor))) ((typep n 'ratio) (* divisor n)))
> How about
>
> (do ((n dividend (/ n divisor))) ((not (zerop (rem n divisor))) n))
>
> Just make sure you don't pass 0 as either of the inputs....
> There may also be a more direct way to compute this, but I can't think of one at the moment.

Of course, if you goal is efficiency, my quick solution actually has more divisions in it
than the original formulation. Since it tests REM (a division) at each step and then
if it doesn't exit performs essentially the same division once again.

There would be other loop formulations using additional variables and more logic that
could preserve the previous value of N and return it, saving a multiplication. But I don't
think you can avoid the extra division.

Re: repeated division returning an integer?

<86353ouryn.fsf@williamsburg.bawden.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17401&group=comp.lang.lisp#17401

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alan@csail.mit.edu (Alan Bawden)
Newsgroups: comp.lang.lisp
Subject: Re: repeated division returning an integer?
Date: Mon, 22 May 2023 14:44:00 -0400
Organization: ITS Preservation Society
Lines: 24
Message-ID: <86353ouryn.fsf@williamsburg.bawden.org>
References: <m38rdh5e91.fsf@carbon.jhcloos.org>
<9728cd6d-33a5-421a-a20c-cfe02a26ff0cn@googlegroups.com>
<70d44559-f878-474a-9ab9-94096ae0c2b4n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="775a891660070c7abb2690e825fd01d4";
logging-data="2364301"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FIiS4YhqfQyHCqdP1g/6K"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock: sha1:tnoES4O1kvZ+yp2dkd2C9DWJ0ds=
sha1:maslom6DGgJfow42/io5LAc8Z60=
 by: Alan Bawden - Mon, 22 May 2023 18:44 UTC

Tom Russ <taruss@google.com> writes:

On Monday, May 22, 2023 at 11:16:13 AM UTC-7, Tom Russ wrote:
> On Sunday, May 21, 2023 at 6:48:56 PM UTC-7, James Cloos wrote:
> > given (declare (integer dividend divisor)), this works:
> >
> > (do (( n dividend (/ n divisor))) ((typep n 'ratio) (* divisor n)))
> How about
>
> (do ((n dividend (/ n divisor))) ((not (zerop (rem n divisor))) n))
>
> Just make sure you don't pass 0 as either of the inputs.... There
> may also be a more direct way to compute this, but I can't think of
> one at the moment.

Of course, if you goal is efficiency, my quick solution actually has
more divisions in it than the original formulation. Since it tests
REM (a division) at each step and then if it doesn't exit performs
essentially the same division once again.

I think the function you are looking for is the two-argument version of
FLOOR, which returns both the quotient and the remainder.

- Alan

Re: repeated division returning an integer?

<20230522121800.965@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17402&group=comp.lang.lisp#17402

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp
Subject: Re: repeated division returning an integer?
Date: Mon, 22 May 2023 19:39:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <20230522121800.965@kylheku.com>
References: <m38rdh5e91.fsf@carbon.jhcloos.org>
Injection-Date: Mon, 22 May 2023 19:39:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="43f5b8a9c95ec5b64b5f6cfacb63a26a";
logging-data="2374955"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+tlQ+zyBGWxASBBs5ELXHpGk4p7fiMBJI="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:otn8J9GmZD3p7tPt38lqYZQ50lM=
 by: Kaz Kylheku - Mon, 22 May 2023 19:39 UTC

On 2023-05-22, James Cloos <cloos@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: @Kazinator@mstdn.ca

Re: repeated division returning an integer?

<m35y8gwcvb.fsf@carbon.jhcloos.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17404&group=comp.lang.lisp#17404

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: cloos@jhcloos.com (James Cloos)
Newsgroups: comp.lang.lisp
Subject: Re: repeated division returning an integer?
Date: Thu, 25 May 2023 13:16:08 -0400
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <m35y8gwcvb.fsf@carbon.jhcloos.org>
References: <m38rdh5e91.fsf@carbon.jhcloos.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="77bcc63abea0fe22a1eae3cbb2dad657";
logging-data="3791202"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199V8Hhbk37RdsSfdARduVa"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:23T0I+DbNoki+GMkE1bSgu9c9P4=
sha1:XJwnTrtRXsgiQbtrjiEiygqPHbw=
OpenPGP-Fingerprint: E9E9 F828 61A4 6EA9 0F2B 63E7 997A 9F17 ED7D AEA6
OpenPGP: 0x997A9F17ED7DAEA6; url=https://jhcloos.com/public_key/0x997A9F17ED7DAEA6.asc
Copyright: Copyright 2023 James Cloos
Face: iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAACVBMVEX///8ZGXBQKKnCrDQ3
AAAAJElEQVQImWNgQAAXzwQg4SKASgAlXIEEiwsSIYBEcLaAtMEAADJnB+kKcKioAAAAAElFTkSu
QmCC
 by: James Cloos - Thu, 25 May 2023 17:16 UTC

thanks for the replies.

very helpful.

i should have thought of using truncate though...

-JimC
--
James Cloos <cloos@jhcloos.com> OpenPGP: 0x997A9F17ED7DAEA6

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor