continued_fractions 1.8.3 → 1.8.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/continued_fractions/include.rb +12 -0
- data/lib/continued_fractions.rb +50 -44
- metadata +9 -9
- data/lib/continued_fractions/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7aa696a6a061c921c1513ffeb7d17ce641a4936e89174e61accd3cf0615ca9f
|
4
|
+
data.tar.gz: 46d89b9a7a7242ec10bd5074858b443ea2b4e01101c6a1f005e512aceb4f880e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3388b0c412ace93cc15958c11afa99e4e08059e2be6f4726c37121c6eded00a725406218b14f8679ab252d6c565c5ccbdb4c9e276cef5c1bd8d6a26cc6edf8c5
|
7
|
+
data.tar.gz: fd8308ba053a0e2c09d146e720b5fd195b76fc78903236bb7335f6520e17551a4598905f6f667f35d046f41486e99969b599de16b96b0feb1a204945857f44b0
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ContinuedFractions
|
2
|
+
module Numeric
|
3
|
+
def to_cf(limit: ContinuedFraction::DEFAULT_LIMIT)
|
4
|
+
ContinuedFraction.new(self, limit)
|
5
|
+
end
|
6
|
+
alias :to_continued_fraction :to_cf
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
Float.include(ContinuedFractions::Numeric)
|
11
|
+
Integer.include(ContinuedFractions::Numeric)
|
12
|
+
Rational.include(ContinuedFractions::Numeric)
|
data/lib/continued_fractions.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
1
|
-
|
1
|
+
require 'continued_fractions/include'
|
2
|
+
|
3
|
+
# ContinuedFraction
|
2
4
|
#
|
3
5
|
# Generates quotients and convergents for a given number
|
4
6
|
#
|
5
7
|
# Author:: Jose Hales-Garcia (mailto:jolohaga@me.com)
|
6
|
-
# Copyright:: Copyright (c)
|
8
|
+
# Copyright:: Copyright (c) 2022 Jose Hales-Garcia
|
7
9
|
#
|
8
10
|
#
|
9
11
|
class ContinuedFraction
|
10
12
|
include ::Comparable
|
11
13
|
|
12
14
|
attr_accessor :number, :quotients, :limit
|
13
|
-
|
15
|
+
|
16
|
+
DEFAULT_LIMIT = 5
|
17
|
+
|
14
18
|
# For a given number calculate its continued fraction quotients and convergents up to limit.
|
15
19
|
#
|
16
20
|
# The limit is 5 by default. Pass an integer in the second parameter to change it.
|
@@ -22,13 +26,12 @@ class ContinuedFraction
|
|
22
26
|
# @convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
|
23
27
|
# [355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>
|
24
28
|
#
|
25
|
-
def initialize(number,limit=
|
29
|
+
def initialize(number, limit=DEFAULT_LIMIT)
|
26
30
|
@number = number
|
27
|
-
@
|
28
|
-
@quotients =
|
29
|
-
@convergents = calculate_convergents
|
31
|
+
@ratioed_number = number.to_r
|
32
|
+
@quotients, @convergents, @limit = calculate_quotients_and_convergents(number, limit)
|
30
33
|
end
|
31
|
-
|
34
|
+
|
32
35
|
# Return nth convergent.
|
33
36
|
#
|
34
37
|
# Example:
|
@@ -40,7 +43,7 @@ class ContinuedFraction
|
|
40
43
|
raise(IndexError, "Convergent index must be greater than zero.") unless nth > 0
|
41
44
|
convergents[nth-1]
|
42
45
|
end
|
43
|
-
|
46
|
+
|
44
47
|
# Return array of convergents of the continued fraction instance up to the nth convergent as an array
|
45
48
|
# comprising of numerators in [i,0] and denominators in [i,1].
|
46
49
|
# If nth is nil, then return the entire list.
|
@@ -59,7 +62,7 @@ class ContinuedFraction
|
|
59
62
|
nth ||= @convergents.length
|
60
63
|
@convergents[0...nth]
|
61
64
|
end
|
62
|
-
|
65
|
+
|
63
66
|
# Return array of convergents of the continued fraction instance converted as Rationals.
|
64
67
|
# If nth is nil, then return the entire list.
|
65
68
|
#
|
@@ -77,25 +80,25 @@ class ContinuedFraction
|
|
77
80
|
nth ||= convergents.length
|
78
81
|
convergents[0...nth].map{|convergent| convergent_to_rational(convergent)}
|
79
82
|
end
|
80
|
-
|
83
|
+
|
81
84
|
def +(other)
|
82
85
|
number_of(other) do |num,prec|
|
83
86
|
evaluate("#{number} + #{num}",prec)
|
84
87
|
end
|
85
88
|
end
|
86
|
-
|
89
|
+
|
87
90
|
def -(other)
|
88
91
|
number_of(other) do |num,prec|
|
89
92
|
evaluate("#{number} - #{num}",prec)
|
90
93
|
end
|
91
94
|
end
|
92
|
-
|
95
|
+
|
93
96
|
def /(other)
|
94
97
|
number_of(other) do |num,prec|
|
95
98
|
evaluate("#{number} / #{num}",prec)
|
96
99
|
end
|
97
100
|
end
|
98
|
-
|
101
|
+
|
99
102
|
def *(other)
|
100
103
|
number_of(other) do |num,prec|
|
101
104
|
evaluate("#{number} * #{num}",prec)
|
@@ -105,11 +108,11 @@ class ContinuedFraction
|
|
105
108
|
def <=>(other)
|
106
109
|
number <=> other.number
|
107
110
|
end
|
108
|
-
|
111
|
+
|
109
112
|
def convergent_to_rational(convergent) #:nodoc:
|
110
113
|
Rational(convergent[0],convergent[1])
|
111
114
|
end
|
112
|
-
|
115
|
+
|
113
116
|
private
|
114
117
|
def evaluate(exp, prec) #:nodoc:
|
115
118
|
ContinuedFraction.new(eval(exp), prec)
|
@@ -129,40 +132,43 @@ class ContinuedFraction
|
|
129
132
|
yield(num,prec)
|
130
133
|
end
|
131
134
|
|
132
|
-
def
|
133
|
-
|
134
|
-
|
135
|
-
limit.times do |i|
|
136
|
-
qs[i] = n.to_i
|
137
|
-
break if ((divisor = n-qs[i]) == 0.0)
|
138
|
-
n = 1.0/divisor
|
139
|
-
end
|
140
|
-
self.limit = qs.compact.length
|
141
|
-
end.compact
|
142
|
-
end
|
135
|
+
def calculate_quotients_and_convergents(x, limit)
|
136
|
+
_quotients = []
|
137
|
+
_convergents = []
|
143
138
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
139
|
+
# Initialize the initial values for p and q
|
140
|
+
p_minus_1, q_minus_1 = 0, 1
|
141
|
+
p_0, q_0 = 1, 0
|
142
|
+
|
143
|
+
n = x.to_i
|
144
|
+
|
145
|
+
# Loop for maximum specified terms or until the fractional part becomes zero
|
146
|
+
limit.times do
|
147
|
+
_quotients << n
|
148
|
+
|
149
|
+
# Calculate the new p and q
|
150
|
+
p_n = n * p_0 + p_minus_1
|
151
|
+
q_n = n * q_0 + q_minus_1
|
152
|
+
|
153
|
+
_convergents << [p_n, q_n]
|
154
|
+
|
155
|
+
# Recalculate the fractional part
|
156
|
+
x = 1.0 / (x - n)
|
157
|
+
break if x.infinite? || _convergents.include?([@ratioed_number.numerator, @ratioed_number.denominator])
|
158
|
+
|
159
|
+
# Update the old values
|
160
|
+
p_minus_1, q_minus_1 = p_0, q_0
|
161
|
+
p_0, q_0 = p_n, q_n
|
162
|
+
n = x.to_i
|
159
163
|
end
|
164
|
+
|
165
|
+
return _quotients, _convergents, _quotients.length
|
160
166
|
end
|
161
167
|
end
|
162
168
|
|
163
169
|
# @return [ContinuedFraction] new instance
|
164
170
|
# @see ContinuedFraction#initialize
|
165
171
|
#
|
166
|
-
def ContinuedFraction(number,limit=
|
167
|
-
ContinuedFraction.new(number,limit
|
172
|
+
def ContinuedFraction(number,limit=ContinuedFraction::DEFAULT_LIMIT)
|
173
|
+
ContinuedFraction.new(number,limit)
|
168
174
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: continued_fractions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jose Hales-Garcia
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '11.1'
|
41
41
|
description: Class for working with continued fractions
|
42
|
-
email:
|
42
|
+
email: jose@halesgarcia.com
|
43
43
|
executables: []
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files:
|
@@ -50,13 +50,13 @@ files:
|
|
50
50
|
- LICENSE
|
51
51
|
- README.md
|
52
52
|
- lib/continued_fractions.rb
|
53
|
-
- lib/continued_fractions/
|
54
|
-
homepage:
|
53
|
+
- lib/continued_fractions/include.rb
|
54
|
+
homepage: https://jolohaga.github.io/continued_fractions
|
55
55
|
licenses:
|
56
56
|
- MIT
|
57
57
|
metadata:
|
58
58
|
source_code_uri: https://github.com/jolohaga/continued_fractions
|
59
|
-
post_install_message:
|
59
|
+
post_install_message:
|
60
60
|
rdoc_options:
|
61
61
|
- "--line-numbers"
|
62
62
|
- "--title"
|
@@ -76,8 +76,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '1.2'
|
78
78
|
requirements: []
|
79
|
-
rubygems_version: 3.
|
80
|
-
signing_key:
|
79
|
+
rubygems_version: 3.4.19
|
80
|
+
signing_key:
|
81
81
|
specification_version: 4
|
82
82
|
summary: Generate continued fractions
|
83
83
|
test_files: []
|