Skip to content

Commit d9ee0ef

Browse files
committed
new file: Math/convergents_to_cube_root_of_2.sf
1 parent 60506d0 commit d9ee0ef

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Math/convergents_to_cube_root_of_2.sf

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/ruby
2+
3+
# Compute the convergents to cube root of 2.
4+
5+
# Algorithm due to Robert Israel, Oct 08 2017.
6+
7+
# See also:
8+
# https://oeis.org/A002352 -- Numerators of convergents to cube root of 2.
9+
# https://oeis.org/A002351 -- Denominators of convergents to cube root of 2.
10+
11+
var (a, c, q)
12+
13+
c = -> (n) is cached {
14+
15+
return 1 if (n == 0)
16+
17+
floor((-1)**n * 3*a(n)**2 / (q(n)*(a(n)**3 - 2*q(n)**3)) - q(n-1)/q(n))
18+
}
19+
20+
a = -> (n) is cached {
21+
22+
return 1 if (n == 0)
23+
return 1 if (n == 1)
24+
25+
c(n-1)*a(n-1) + a(n-2)
26+
}
27+
28+
q = -> (n) is cached {
29+
30+
return 0 if (n == 0)
31+
return 1 if (n == 1)
32+
33+
c(n-1)*q(n-1) + q(n-2)
34+
}
35+
36+
say ("Numerators : ", 15.of { a(_) })
37+
say ("Denominators: ", 15.of { q(_) })
38+
39+
say ''
40+
41+
for n in (1..20) {
42+
printf("2^(1/3) =~ %22s =~ %s\n", as_rat(a(n)/q(n)) a(n)/q(n))
43+
}
44+
45+
__END__
46+
Numerators : [1, 1, 4, 5, 29, 34, 63, 286, 349, 635, 5429, 6064, 90325, 96389, 1054215]
47+
Denominators: [0, 1, 3, 4, 23, 27, 50, 227, 277, 504, 4309, 4813, 71691, 76504, 836731]
48+
49+
2^(1/3) =~ 1 =~ 1
50+
2^(1/3) =~ 4/3 =~ 1.33333333333333333333333333333333333333333333333
51+
2^(1/3) =~ 5/4 =~ 1.25
52+
2^(1/3) =~ 29/23 =~ 1.26086956521739130434782608695652173913043478261
53+
2^(1/3) =~ 34/27 =~ 1.25925925925925925925925925925925925925925925926
54+
2^(1/3) =~ 63/50 =~ 1.26
55+
2^(1/3) =~ 286/227 =~ 1.25991189427312775330396475770925110132158590308
56+
2^(1/3) =~ 349/277 =~ 1.25992779783393501805054151624548736462093862816
57+
2^(1/3) =~ 635/504 =~ 1.25992063492063492063492063492063492063492063492
58+
2^(1/3) =~ 5429/4309 =~ 1.25992109538175910884195869111162682757020190299
59+
2^(1/3) =~ 6064/4813 =~ 1.25992104716393102015375025971327654269686266362
60+
2^(1/3) =~ 90325/71691 =~ 1.2599210500620719476642814300260841667712823088
61+
2^(1/3) =~ 96389/76504 =~ 1.2599210498797448499424866673637979713479033776
62+
2^(1/3) =~ 1054215/836731 =~ 1.25992104989536661125260089562834411537280201164
63+
2^(1/3) =~ 2204819/1749966 =~ 1.25992104989468366813983814542682543546560333172
64+
2^(1/3) =~ 3259034/2586697 =~ 1.25992104989490458294883397630259748242643030861
65+
2^(1/3) =~ 15240955/12096754 =~ 1.25992104989487262450736784429938808377850785426
66+
2^(1/3) =~ 186150494/147747745 =~ 1.25992104989487318402050738574724101542125059168
67+
2^(1/3) =~ 387541943/307592244 =~ 1.25992104989487316201639986735166183188936324415
68+
2^(1/3) =~ 1348776323/1070524477 =~ 1.2599210498948731650532825696427303642119338482

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ A simple collection of Sidef scripts.
112112
* [Continued fraction to fraction](./Math/continued_fraction_to_fraction.sf)
113113
* [Continued fractions convergents](./Math/continued_fractions_convergents.sf)
114114
* [Continued fractions for nth roots](./Math/continued_fractions_for_nth_roots.sf)
115+
* [Convergents to cube root of 2](./Math/convergents_to_cube_root_of_2.sf)
115116
* [Cousin mersenne primes lucas-lehmer](./Math/cousin_mersenne_primes_lucas-lehmer.sf)
116117
* [Cuban primes](./Math/cuban_primes.sf)
117118
* [Difference of two squares solutions](./Math/difference_of_two_squares_solutions.sf)

0 commit comments

Comments
 (0)