Skip to content

Commit 472490c

Browse files
committed
new file: Math/pell_method_for_square_roots.sf
1 parent d9ee0ef commit 472490c

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

Math/convergents_to_cube_root_of_2.sf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
# Compute the convergents to cube root of 2.
44

5-
# Algorithm due to Robert Israel, Oct 08 2017.
5+
# Formula due to Enrico Bombieri and Alfred J. van der Poorten:
6+
# https://web.williams.edu/Mathematics/sjmiller/public_html/book/papers/vdp/BombieriPoorten_CFofAlgNumbs.pdf
7+
8+
# Algorithm adapted after Robert Israel, Oct 08 2017.
69

710
# See also:
811
# https://oeis.org/A002352 -- Numerators of convergents to cube root of 2.

Math/pell_method_for_square_roots.sf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/ruby
2+
3+
# Daniel "Trizen" Șuteu
4+
# Date: 04 February 2019
5+
# https://github.com/trizen
6+
7+
# Approximate square roots, using Pell's method.
8+
9+
# See also:
10+
# https://rosettacode.org/wiki/Pell%27s_equation
11+
# https://en.wikipedia.org/wiki/Pell%27s_equation
12+
# https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
13+
14+
func pell_square_root(n, eps=1e-22) {
15+
16+
var x = n.isqrt
17+
var y = x
18+
var z = 1
19+
var r = 2*x
20+
21+
var (e1, e2) = (1, 0)
22+
var (f1, f2) = (0, 1)
23+
24+
loop {
25+
26+
y = (r*z - y)
27+
z = ((n - y*y) / z)
28+
r = round((x + y) / z) # floor() also works
29+
30+
(e1, e2) = (e2, r*e2 + e1)
31+
(f1, f2) = (f2, r*f2 + f1)
32+
33+
var A = (e2 + x*f2)
34+
var B = f2
35+
36+
if (abs((A/B)**2 - n) <= eps) {
37+
return A/B
38+
}
39+
}
40+
}
41+
42+
for n in [61, 109, 181, 277] {
43+
var s = pell_square_root(n)
44+
say "sqrt(#{'%3d' % n}) =~ #{s.as_rat} =~ #{s}"
45+
}
46+
47+
__END__
48+
sqrt( 61) =~ 5380205503727/688864726095 =~ 7.81024967590665439412972327539046655850673601909
49+
sqrt(109) =~ 5886776254306/563850903159 =~ 10.4403065089105501797577550769986386680615397574
50+
sqrt(181) =~ 2900300962727/215577672795 =~ 13.45362404707371031716308866094140080774035985437
51+
sqrt(277) =~ 4157003026204/249770104837 =~ 16.64331697709323806892821623002200061329832127014

Math/sqrt_convergents.sf

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ func sqrt_convergents (n, callback, count=10) {
2020
count.times {
2121

2222
y = (r*z - y)
23-
z = ((n - y*y) // z) #/
24-
r = ((x + y) // z) #/
23+
z = ((n - y*y) / z)
24+
r = ((x + y) // z) #/
2525

2626
callback(e2 + x*f2, f2)
2727

2828
(f1, f2) = (f2, r*f2 + f1)
2929
(e1, e2) = (e2, r*e2 + e1)
30-
31-
y = x if (z == 1)
3230
}
3331
}
3432

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ A simple collection of Sidef scripts.
252252
* [Partial sums of sigma function times k^m](./Math/partial_sums_of_sigma_function_times_k^m.sf)
253253
* [Partial sums sublinear formula](./Math/partial_sums_sublinear_formula.sf)
254254
* [Pell factorization](./Math/pell_factorization.sf)
255+
* [Pell method for square roots](./Math/pell_method_for_square_roots.sf)
255256
* [Perfect squares in catalan's triangle](./Math/perfect_squares_in_catalan_s_triangle.sf)
256257
* [Permutations iter](./Math/permutations_iter.sf)
257258
* [Permutations rec](./Math/permutations_rec.sf)

0 commit comments

Comments
 (0)