From 1954637450934c05f1f240f52a79d3a6b1a89a9c Mon Sep 17 00:00:00 2001 From: Benjamin Qi Date: Sat, 26 Nov 2022 00:51:14 -0500 Subject: [PATCH 1/3] fix continued fraction sqrt code --- src/algebra/continued-fractions.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/algebra/continued-fractions.md b/src/algebra/continued-fractions.md index 3f4ca46b2..0e865afea 100644 --- a/src/algebra/continued-fractions.md +++ b/src/algebra/continued-fractions.md @@ -678,14 +678,15 @@ One can further prove (and it was first done by Lagrange) that for arbitrary qua ```py # compute the continued fraction of sqrt(n) def sqrt(n): - n0 = math.floor(math.sqrt(n)) x, y, z = 0, 1, 1 a = [] def step(x, y, z): - a.append((x * n0 + y) // z) - t = y - a[-1]*z - x, y, z = z*t, -z*y, t**2 - n*x**2 + a.append(math.floor(x + y * math.sqrt(n)) // z) + t = x - a[-1]*z + x, y, z = z*t, -z*y, t**2 - n*y**2 g = math.gcd(x, math.gcd(y, z)) + if z < 0: + g *= -1 return x // g, y // g, z // g used = dict() From eeacac711ceb2468dd5b4d1a5d464864784a9cdd Mon Sep 17 00:00:00 2001 From: Oleksandr Kulkov Date: Tue, 29 Nov 2022 02:17:19 +0100 Subject: [PATCH 2/3] proper initialization for x, y, z --- src/algebra/continued-fractions.md | 33 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/algebra/continued-fractions.md b/src/algebra/continued-fractions.md index 0e865afea..d3f555ef4 100644 --- a/src/algebra/continued-fractions.md +++ b/src/algebra/continued-fractions.md @@ -678,23 +678,22 @@ One can further prove (and it was first done by Lagrange) that for arbitrary qua ```py # compute the continued fraction of sqrt(n) def sqrt(n): - x, y, z = 0, 1, 1 - a = [] - def step(x, y, z): - a.append(math.floor(x + y * math.sqrt(n)) // z) - t = x - a[-1]*z - x, y, z = z*t, -z*y, t**2 - n*y**2 - g = math.gcd(x, math.gcd(y, z)) - if z < 0: - g *= -1 - return x // g, y // g, z // g - - used = dict() - for i in range(n): - used[x, y, z] = i - x, y, z = step(x, y, z) - if (x, y, z) in used: - return a + n0 = math.floor(math.sqrt(n)) + x, y, z = 1, 0, 1 + a = [] + def step(x, y, z): + a.append((x * n0 + y) // z) + t = y - a[-1]*z + x, y, z = -z*x, z*t, t**2 - n*x**2 + g = math.gcd(x, math.gcd(y, z)) + return x // g, y // g, z // g + + used = dict() + for i in range(n): + used[x, y, z] = i + x, y, z = step(x, y, z) + if (x, y, z) in used: + return a ``` Using the same `step` function but different initial $x$, $y$ and $z$ it is possible to compute it for arbitrary $\frac{x+y \sqrt{n}}{z}$. From 2133fa30d0a9b23f18e018c2d9df72259f693d54 Mon Sep 17 00:00:00 2001 From: Oleksandr Kulkov Date: Tue, 29 Nov 2022 02:18:15 +0100 Subject: [PATCH 3/3] extra tab --- src/algebra/continued-fractions.md | 32 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/algebra/continued-fractions.md b/src/algebra/continued-fractions.md index d3f555ef4..c0c5a97fe 100644 --- a/src/algebra/continued-fractions.md +++ b/src/algebra/continued-fractions.md @@ -678,22 +678,22 @@ One can further prove (and it was first done by Lagrange) that for arbitrary qua ```py # compute the continued fraction of sqrt(n) def sqrt(n): - n0 = math.floor(math.sqrt(n)) - x, y, z = 1, 0, 1 - a = [] - def step(x, y, z): - a.append((x * n0 + y) // z) - t = y - a[-1]*z - x, y, z = -z*x, z*t, t**2 - n*x**2 - g = math.gcd(x, math.gcd(y, z)) - return x // g, y // g, z // g - - used = dict() - for i in range(n): - used[x, y, z] = i - x, y, z = step(x, y, z) - if (x, y, z) in used: - return a + n0 = math.floor(math.sqrt(n)) + x, y, z = 1, 0, 1 + a = [] + def step(x, y, z): + a.append((x * n0 + y) // z) + t = y - a[-1]*z + x, y, z = -z*x, z*t, t**2 - n*x**2 + g = math.gcd(x, math.gcd(y, z)) + return x // g, y // g, z // g + + used = dict() + for i in range(n): + used[x, y, z] = i + x, y, z = step(x, y, z) + if (x, y, z) in used: + return a ``` Using the same `step` function but different initial $x$, $y$ and $z$ it is possible to compute it for arbitrary $\frac{x+y \sqrt{n}}{z}$. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy