continued_fractions 1.8.6 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +9 -16
- data/lib/continued_fractions/include.rb +7 -1
- data/lib/continued_fractions.rb +47 -80
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3fc86b49a35fff8bfffba3ee95a21ee8f2c09e30e4bba04a25aac57a49cacfef
|
4
|
+
data.tar.gz: 27b47d0bd7c4e75bb89741bf383dad99c2bd5f4919d7fb5c47936f8de442e175
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e915584d227d3fd295ff5ad5a09a11e0dbfa1073e61761d5edcb6ab229a6b7b3b855260265fcb816c5847e4304c2463b85927b9bfb2a7ac3076b4bd8dd75b778
|
7
|
+
data.tar.gz: 1756e4b64ad3a71db1852db5c8faa6a42cbc99f0223a771e870cc19ec1f2135f0eafd5d1ad35f4235c8f56696f45cd6e27c818696392441ae10f4dae716ac0d2
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -14,29 +14,22 @@ Generates quotients and convergents for a given number.
|
|
14
14
|
=> 3.14159265358979
|
15
15
|
|
16
16
|
cf = ContinuedFraction.new(pi,10)
|
17
|
-
=>
|
18
|
-
@quotients=[3, 7, 15, 1, 292, 1, 1, 1, 2, 1],
|
19
|
-
@convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
|
20
|
-
[355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>
|
17
|
+
=> 3.141592653589793, quotients: [3, 7, 15, 1, 292, 1, 1, 1, 2, 1], convergents: [(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
21
18
|
|
22
19
|
cf.convergents
|
23
|
-
=> [
|
24
|
-
|
25
|
-
# Convergents can be output as an array of Rationals too
|
26
|
-
cf.convergents_as_rationals
|
27
|
-
[(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
28
|
-
|
20
|
+
=> [(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
21
|
+
|
29
22
|
cf.convergent(1)
|
30
|
-
=>
|
23
|
+
=> (22/7)
|
31
24
|
|
32
25
|
cf.quotients
|
33
26
|
=> [3, 7, 15, 1, 292, 1, 1, 1, 2, 1]
|
34
27
|
|
35
28
|
# Add, subtract, multiply and divide continued fractions. The limit of the result is the max limit of the operands.
|
36
29
|
cf2 = ContinuedFraction.new(rand,20)
|
37
|
-
=>
|
30
|
+
=> 0.9465929215086878, quotients: [0, 1, 17, 1, 2, 1, 1, 1, 1, 1, 41, 1, 1, 2, 22, 1, 1, 11, 1, 3], convergents: [(0/1), (1/1), (17/18), (18/19), (53/56), (71/75), (124/131), (195/206), (319/337), (514/543), (21393/22600), (21907/23143), (43300/45743), (108507/114629), (2430454/2567581), (2538961/2682210), (4969415/5249791), (57202526/60429911), (62171941/65679702), (243718349/257469017)]
|
38
31
|
cf + cf2
|
39
|
-
=>
|
32
|
+
=> 4.088185575098481, quotients: [4, 11, 2, 1, 16, 1, 2, 1, 1, 1, 1, 25, 7, 1, 3, 1, 3], convergents: [(4/1), (45/11), (94/23), (139/34), (2318/567), (2457/601), (7232/1769), (9689/2370), (16921/4139), (26610/6509), (43531/10648), (1114885/272709), (7847726/1919611), (8962611/2192320), (34735559/8496571), (43698170/10688891), (165830069/40563244)]
|
40
33
|
|
41
34
|
## Install
|
42
35
|
|
@@ -44,14 +37,14 @@ sudo gem install continued_fractions
|
|
44
37
|
|
45
38
|
## Requirements/Dependencies
|
46
39
|
|
47
|
-
* Ruby
|
40
|
+
* Ruby 3.1+
|
48
41
|
* Depends on Ruby's Rational class
|
49
42
|
|
50
43
|
## Developers
|
51
44
|
|
52
45
|
* RSpec required to run tests
|
46
|
+
* Yard to compile documentation
|
53
47
|
|
54
48
|
## Contributors
|
55
49
|
|
56
|
-
* [Jose Hales-Garcia](https://github.com/jolohaga)
|
57
|
-
* [Eoin Kelly](https://github.com/eoinkelly)
|
50
|
+
* [Jose Hales-Garcia](https://github.com/jolohaga)
|
@@ -1,9 +1,15 @@
|
|
1
1
|
module ContinuedFractions
|
2
2
|
module Numeric
|
3
|
+
|
4
|
+
# @return [ContinuedFraction]
|
5
|
+
# @example
|
6
|
+
# 1.5.to_cf
|
7
|
+
# => 1.5, quotients: [1, 2], convergents: [(1/1), (3/2)]
|
8
|
+
# @param limit [Float]
|
9
|
+
#
|
3
10
|
def to_cf(limit: ContinuedFraction::DEFAULT_LIMIT)
|
4
11
|
ContinuedFraction.new(self, limit)
|
5
12
|
end
|
6
|
-
alias :to_continued_fraction :to_cf
|
7
13
|
end
|
8
14
|
end
|
9
15
|
|
data/lib/continued_fractions.rb
CHANGED
@@ -4,101 +4,67 @@ require 'continued_fractions/include'
|
|
4
4
|
#
|
5
5
|
# Generates quotients and convergents for a given number
|
6
6
|
#
|
7
|
-
#
|
8
|
-
# Copyright:: Copyright (c) 2023 Jose Hales-Garcia
|
7
|
+
# @author Jose Hales-Garcia
|
9
8
|
#
|
9
|
+
# @attr_reader number [Numeric]
|
10
|
+
# The number who's continued fraction is being calculated
|
11
|
+
# @attr_reader quotients [Array]
|
12
|
+
# The array of quotients of the continued fraction
|
13
|
+
# @attr_reader convergents [Array]
|
14
|
+
# The convergents of the continued fraction
|
15
|
+
# @attr_reader limit [Integer]
|
16
|
+
# The limit to calculate. Practical for real numbers, although they're limited by the system.
|
10
17
|
#
|
11
18
|
class ContinuedFraction
|
12
19
|
include Comparable
|
13
20
|
|
14
|
-
|
21
|
+
attr_reader :number, :quotients, :convergents, :limit
|
15
22
|
|
16
23
|
DEFAULT_LIMIT = 5
|
17
24
|
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
# => #<ContinuedFractions::ContinuedFraction:0x000001010ed5c8 @number=3.14159265358979, @limit=10,
|
25
|
-
# @quotients=[3, 7, 15, 1, 292, 1, 1, 1, 2, 1],
|
26
|
-
# @convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
|
27
|
-
# [355, 113], [103993, 33102], [104348, 33215], [208341, 66317], [312689, 99532], [833719, 265381], [1146408, 364913]]>
|
25
|
+
# @return [ContinuedFraction] the quotients and convergents of a continued fraction for a given number.
|
26
|
+
# @example
|
27
|
+
# ContinuedFraction.new(Math::PI,10)
|
28
|
+
# => 3.141592653589793, quotients: [3, 7, 15, 1, 292, 1, 1, 1, 2, 1], convergents: [(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
29
|
+
# @param number [Numeric]
|
30
|
+
# @param limit [Integer]
|
28
31
|
#
|
29
32
|
def initialize(number, limit=DEFAULT_LIMIT)
|
30
33
|
@number = number
|
31
34
|
@ratioed_number = number.to_r
|
32
|
-
@quotients, @convergents, @limit = calculate_quotients_and_convergents(
|
33
|
-
end
|
34
|
-
|
35
|
-
# Return nth convergent.
|
36
|
-
#
|
37
|
-
# Example:
|
38
|
-
# cf = ContinuedFraction.new(Math::PI,10)
|
39
|
-
# cf.convergent(3)
|
40
|
-
# => [355,113]
|
41
|
-
#
|
42
|
-
def convergent(nth)
|
43
|
-
raise(IndexError, "Convergent index must be greater than zero.") unless nth > 0
|
44
|
-
convergents[nth-1]
|
35
|
+
@quotients, @convergents, @limit = calculate_quotients_and_convergents(limit)
|
45
36
|
end
|
46
37
|
|
47
|
-
#
|
48
|
-
#
|
49
|
-
# If nth is nil, then return the entire list.
|
50
|
-
#
|
51
|
-
# Example:
|
52
|
-
# cf = ContinuedFraction.new(Math::PI,10)
|
53
|
-
# cf.convergents
|
54
|
-
# => [[3,1], [22,7], [333,106], [355,113], [103993,33102],
|
55
|
-
# [104348,33215], [208341,66317], [312689,99532], [833719,265381], [1146408,364913]]
|
56
|
-
#
|
57
|
-
# Or:
|
58
|
-
# cf.convergents(3)
|
59
|
-
# => [[3,1], [22,7], [333,106]]
|
60
|
-
#
|
61
|
-
def convergents(nth=nil)
|
62
|
-
nth ||= @convergents.length
|
63
|
-
@convergents[0...nth]
|
64
|
-
end
|
65
|
-
|
66
|
-
# Return array of convergents of the continued fraction instance converted as Rationals.
|
67
|
-
# If nth is nil, then return the entire list.
|
68
|
-
#
|
69
|
-
# Example:
|
70
|
-
# cf = ContinuedFraction.new(Math::PI,10)
|
71
|
-
# cf.convergents_as_rationals
|
72
|
-
# => [(3/1), (22/7), (333/106), (355/113), (103993/33102),
|
73
|
-
# (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
38
|
+
# @return [ContinuedFraction] the CF sum of self and another CF
|
39
|
+
# @param other [ContinuedFraction]
|
74
40
|
#
|
75
|
-
# Or:
|
76
|
-
# cf.convergents_as_rationals(3)
|
77
|
-
# => [(3/1), (22/7), (333/106)]
|
78
|
-
#
|
79
|
-
def convergents_as_rationals(nth=nil)
|
80
|
-
nth ||= convergents.length
|
81
|
-
convergents[0...nth].map{|convergent| convergent_to_rational(convergent)}
|
82
|
-
end
|
83
|
-
|
84
41
|
def +(other)
|
85
42
|
number_of(other) do |num,prec|
|
86
43
|
evaluate("#{number} + #{num}",prec)
|
87
44
|
end
|
88
45
|
end
|
89
46
|
|
47
|
+
# @return [ContinuedFraction] the CF difference of self and another CF
|
48
|
+
# @param other [ContinuedFraction]
|
49
|
+
#
|
90
50
|
def -(other)
|
91
51
|
number_of(other) do |num,prec|
|
92
52
|
evaluate("#{number} - #{num}",prec)
|
93
53
|
end
|
94
54
|
end
|
95
55
|
|
56
|
+
# @return [ContinuedFraction] the CF quotient of self and another CF
|
57
|
+
# @param other [ContinuedFraction]
|
58
|
+
#
|
96
59
|
def /(other)
|
97
60
|
number_of(other) do |num,prec|
|
98
61
|
evaluate("#{number} / #{num}",prec)
|
99
62
|
end
|
100
63
|
end
|
101
64
|
|
65
|
+
# @return [ContinuedFraction] the CF product of self and another CF
|
66
|
+
# @param other [ContinuedFraction]
|
67
|
+
#
|
102
68
|
def *(other)
|
103
69
|
number_of(other) do |num,prec|
|
104
70
|
evaluate("#{number} * #{num}",prec)
|
@@ -109,8 +75,8 @@ class ContinuedFraction
|
|
109
75
|
number <=> other.number
|
110
76
|
end
|
111
77
|
|
112
|
-
def
|
113
|
-
|
78
|
+
def inspect
|
79
|
+
"#{number}, quotients: #{quotients}, convergents: #{convergents[0..-1]}"
|
114
80
|
end
|
115
81
|
|
116
82
|
private
|
@@ -132,43 +98,44 @@ class ContinuedFraction
|
|
132
98
|
yield(num,prec)
|
133
99
|
end
|
134
100
|
|
135
|
-
def calculate_quotients_and_convergents(
|
101
|
+
def calculate_quotients_and_convergents(limit)
|
102
|
+
x = @number
|
103
|
+
|
136
104
|
_quotients = []
|
137
|
-
_convergents = []
|
105
|
+
_convergents = [] #Convergents.new
|
138
106
|
|
139
107
|
# Initialize the initial values for p and q
|
140
108
|
p_minus_1, q_minus_1 = 0, 1
|
141
109
|
p_0, q_0 = 1, 0
|
142
110
|
|
143
|
-
|
111
|
+
q = x.to_i
|
144
112
|
|
145
113
|
# Loop for maximum specified terms or until the fractional part becomes zero
|
146
114
|
limit.times do
|
147
|
-
_quotients <<
|
115
|
+
_quotients << q
|
148
116
|
|
149
117
|
# Calculate the new p and q
|
150
|
-
p_n =
|
151
|
-
q_n =
|
118
|
+
p_n = q * p_0 + p_minus_1
|
119
|
+
q_n = q * q_0 + q_minus_1
|
120
|
+
|
121
|
+
convergent = Rational(p_n, q_n)
|
122
|
+
_convergents << convergent
|
123
|
+
|
124
|
+
break if ((@number - convergent).to_f).zero?
|
152
125
|
|
153
|
-
|
126
|
+
x_rem = x - q
|
127
|
+
break if x_rem.zero?
|
154
128
|
|
155
129
|
# Recalculate the fractional part
|
156
|
-
x = 1
|
130
|
+
x = 1/1r / x_rem
|
157
131
|
break if x.infinite? || _convergents.include?([@ratioed_number.numerator, @ratioed_number.denominator])
|
158
132
|
|
159
133
|
# Update the old values
|
160
134
|
p_minus_1, q_minus_1 = p_0, q_0
|
161
135
|
p_0, q_0 = p_n, q_n
|
162
|
-
|
136
|
+
q = x.to_i
|
163
137
|
end
|
164
138
|
|
165
139
|
return _quotients, _convergents, _quotients.length
|
166
140
|
end
|
167
141
|
end
|
168
|
-
|
169
|
-
# @return [ContinuedFraction] new instance
|
170
|
-
# @see ContinuedFraction#initialize
|
171
|
-
#
|
172
|
-
def ContinuedFraction(number,limit=ContinuedFraction::DEFAULT_LIMIT)
|
173
|
-
ContinuedFraction.new(number,limit)
|
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:
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jose Hales-Garcia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -69,14 +69,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
69
|
requirements:
|
70
70
|
- - ">="
|
71
71
|
- !ruby/object:Gem::Version
|
72
|
-
version:
|
72
|
+
version: '3.1'
|
73
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
74
|
requirements:
|
75
75
|
- - ">="
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '1.2'
|
78
78
|
requirements: []
|
79
|
-
rubygems_version: 3.4.
|
79
|
+
rubygems_version: 3.4.22
|
80
80
|
signing_key:
|
81
81
|
specification_version: 4
|
82
82
|
summary: Generate continued fractions
|