continued_fractions 1.8.4 → 1.8.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff0ff19ce7e60d7cd554c981adc6ecdc7cfd3591dbfbb0c893bee1b6cc2413fd
4
- data.tar.gz: 9d5fad37f7c8eefa1be24eb210a395e4cbb39b35fa306e4f234f36ce984cfbbb
3
+ metadata.gz: f7aa696a6a061c921c1513ffeb7d17ce641a4936e89174e61accd3cf0615ca9f
4
+ data.tar.gz: 46d89b9a7a7242ec10bd5074858b443ea2b4e01101c6a1f005e512aceb4f880e
5
5
  SHA512:
6
- metadata.gz: 8c2b1dfd1d72ac337d06f69da05975eafcf888e53cb4bfcc864630d73aa89189d3a297f0cd50a5bdcb440e60600a55ae0f0123300e20dbdb9c82a5d3770dff5c
7
- data.tar.gz: b58393bf5eb004fad31e809a9b845eb757bd41919914a4d9afd41a265f8a1eac5461a075edf50a6c20c65c388a9efdfb933b26177baaad028f4671010be478d6
6
+ metadata.gz: 3388b0c412ace93cc15958c11afa99e4e08059e2be6f4726c37121c6eded00a725406218b14f8679ab252d6c565c5ccbdb4c9e276cef5c1bd8d6a26cc6edf8c5
7
+ data.tar.gz: fd8308ba053a0e2c09d146e720b5fd195b76fc78903236bb7335f6520e17551a4598905f6f667f35d046f41486e99969b599de16b96b0feb1a204945857f44b0
@@ -26,11 +26,10 @@ class ContinuedFraction
26
26
  # @convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
27
27
  # [355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>
28
28
  #
29
- def initialize(number,limit=DEFAULT_LIMIT)
29
+ def initialize(number, limit=DEFAULT_LIMIT)
30
30
  @number = number
31
- @limit = limit
32
- @quotients = calculate_quotients
33
- @convergents = calculate_convergents
31
+ @ratioed_number = number.to_r
32
+ @quotients, @convergents, @limit = calculate_quotients_and_convergents(number, limit)
34
33
  end
35
34
 
36
35
  # Return nth convergent.
@@ -133,34 +132,37 @@ class ContinuedFraction
133
132
  yield(num,prec)
134
133
  end
135
134
 
136
- def calculate_quotients #:nodoc:
137
- n = number
138
- Array.new(limit).tap do |qs|
139
- limit.times do |i|
140
- qs[i] = n.to_i
141
- break if ((divisor = n-qs[i]) == 0.0)
142
- n = 1.0/divisor
143
- end
144
- self.limit = qs.compact.length
145
- end.compact
146
- end
135
+ def calculate_quotients_and_convergents(x, limit)
136
+ _quotients = []
137
+ _convergents = []
147
138
 
148
- def calculate_convergents #:nodoc:
149
- nth = nil
150
- convergence_matrix(quotients.length+2,2,1).tap do |convs|
151
- nth ||= convs.length
152
- 2.upto(quotients.length+1) do |i|
153
- i_minus1,i_minus2 = i-1,i-2
154
- convs[i][0] = convs[i_minus1][0]*quotients[i_minus2]+convs[i_minus2][0]
155
- convs[i][1] = convs[i_minus1][1]*quotients[i_minus2]+convs[i_minus2][1]
156
- end
157
- end[2...nth+2]
158
- end
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
159
148
 
160
- def convergence_matrix(n,m,fill=nil) #:nodoc:
161
- Array.new(n).map!{Array.new(m,fill)}.tap do |conv_mat|
162
- conv_mat[0][0],conv_mat[1][1] = 0,0
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
163
163
  end
164
+
165
+ return _quotients, _convergents, _quotients.length
164
166
  end
165
167
  end
166
168
 
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
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: 2022-01-03 00:00:00.000000000 Z
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: jolohaga@me.com
42
+ email: jose@halesgarcia.com
43
43
  executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files:
@@ -51,13 +51,12 @@ files:
51
51
  - README.md
52
52
  - lib/continued_fractions.rb
53
53
  - lib/continued_fractions/include.rb
54
- - lib/continued_fractions/version.rb
55
- homepage: http://jolohaga.github.io/continued_fractions
54
+ homepage: https://jolohaga.github.io/continued_fractions
56
55
  licenses:
57
56
  - MIT
58
57
  metadata:
59
58
  source_code_uri: https://github.com/jolohaga/continued_fractions
60
- post_install_message:
59
+ post_install_message:
61
60
  rdoc_options:
62
61
  - "--line-numbers"
63
62
  - "--title"
@@ -77,8 +76,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
76
  - !ruby/object:Gem::Version
78
77
  version: '1.2'
79
78
  requirements: []
80
- rubygems_version: 3.0.3
81
- signing_key:
79
+ rubygems_version: 3.4.19
80
+ signing_key:
82
81
  specification_version: 4
83
82
  summary: Generate continued fractions
84
83
  test_files: []
@@ -1,3 +0,0 @@
1
- module ContinuedFractions
2
- VERSION = "1.8.4"
3
- end