Sujet : Re: The "Strand" puzzle --- ( Continued Fractions using Lisp or Python? )
De : pc+usenet (at) *nospam* asdf.org (Phil Carmody)
Groupes : rec.puzzlesDate : 20. Aug 2024, 22:30:57
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <8734mzj5ry.fsf@fatphil.org>
References : 1 2 3 4
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)
ilan_no_spew@hotmail.com (IlanMayer) writes:
On Mon, 29 Jul 2024 18:58:21 +0000, HenHanna wrote:
On 7/26/2024 5:37 AM, IlanMayer wrote:
So the solutions to the Strand puzzle can be found from the
continued fraction of \sqrt{2}, which _is_ satisfying simple.
Using Mathematica to look at the first 10 convergents
>
---------- is this (also) easy to do using Lisp or Python???
>
This can be done with Python:
>
N = 10
a = 1
b = 1
print(str(a) + "/" + str(b))
for n in range(N):
temp = a + 2 * b
b = a + b
a = temp
print(str(a) + "/" + str(b))
Let's play golf!
There's no need for temp:
b = b + a
a = 2 * b - a
Pre:
$ perl -e '$a=1;$b=1;$n=0;while($n++<10){print("$a/$b\n");$t=$a+2*$b;$b=$a+$b;$a=$t;}'
1/1
3/2
7/5
17/12
41/29
99/70
239/169
577/408
1393/985
3363/2378
Post:
$ perl -e '$a=1;$b=1;$n=0;while($n++<10){print("$a/$b\n");$b+=$a;$a=2*$b-$a;}'
1/1
3/2
7/5
17/12
41/29
99/70
239/169
577/408
1393/985
3363/2378
Phil
-- We are no longer hunters and nomads. No longer awed and frightened, as we havegained some understanding of the world in which we live. As such, we can castaside childish remnants from the dawn of our civilization.-- NotSanguine on SoylentNews, after Eugen Weber in /The Western Tradition/