continued_fractions 2.0.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -3
- data/lib/continued_fractions.rb +24 -9
- metadata +21 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0dc6cd2a45f726fa6342ebf0efb882e1987e809bdf5527423e824dff1d20e4c2
|
4
|
+
data.tar.gz: c9c654e3bc9de045ceab233afabd54c716e576e968edbaddd020c225104efda6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 718056486dce496d4ba77442b501885f870a030e03cf4c4e86c682ff9000fae47f942f72b6df32991ba32c292339087f1df8f05feb42378442d66b7d21fb4b2f
|
7
|
+
data.tar.gz: a0f29e4141936a1187babdb6b44f9eb712e8f04499e3b82e569321cf96f8cefa034bc5d10d7d1fc5df086b8804ca911e1ace9ffebd332496dbfcc92947766fbc
|
data/README.md
CHANGED
@@ -18,16 +18,15 @@ Generates quotients and convergents for a given number.
|
|
18
18
|
|
19
19
|
cf.convergents
|
20
20
|
=> [(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
|
21
|
-
|
22
|
-
cf.convergent(1)
|
23
|
-
=> (22/7)
|
24
21
|
|
25
22
|
cf.quotients
|
26
23
|
=> [3, 7, 15, 1, 292, 1, 1, 1, 2, 1]
|
27
24
|
|
28
25
|
# Add, subtract, multiply and divide continued fractions. The limit of the result is the max limit of the operands.
|
26
|
+
|
29
27
|
cf2 = ContinuedFraction.new(rand,20)
|
30
28
|
=> 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)]
|
29
|
+
|
31
30
|
cf + cf2
|
32
31
|
=> 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)]
|
33
32
|
|
data/lib/continued_fractions.rb
CHANGED
@@ -6,15 +6,6 @@ require 'continued_fractions/include'
|
|
6
6
|
#
|
7
7
|
# @author Jose Hales-Garcia
|
8
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.
|
17
|
-
#
|
18
9
|
class ContinuedFraction
|
19
10
|
include Comparable
|
20
11
|
|
@@ -71,6 +62,30 @@ class ContinuedFraction
|
|
71
62
|
end
|
72
63
|
end
|
73
64
|
|
65
|
+
# @return [ContinuedFraction] the result from the provided array of quotients
|
66
|
+
# @example
|
67
|
+
# ContinuedFraction.from_quotients(2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10)
|
68
|
+
# => 2.7182818284454013, quotients: [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 9], convergents: [(2/1), (3/1), (8/3), (11/4), (19/7), (87/32), (106/39), (193/71), (1264/465), (1457/536), (2721/1001), (23225/8544), (25946/9545), (49171/18089), (468485/172346)]
|
69
|
+
# @param quotients [Array] or comma separated list of quotients
|
70
|
+
#
|
71
|
+
def self.from_quotients(*quotients)
|
72
|
+
quotients = quotients.flatten
|
73
|
+
return quotients.first if quotients.size == 1
|
74
|
+
|
75
|
+
p_minus_2, p_minus_1 = 0, 1
|
76
|
+
q_minus_2, q_minus_1 = 1, 0
|
77
|
+
|
78
|
+
quotients.each do |quotient|
|
79
|
+
p = quotient * p_minus_1 + p_minus_2
|
80
|
+
q = quotient * q_minus_1 + q_minus_2
|
81
|
+
|
82
|
+
p_minus_2, p_minus_1 = p_minus_1, p
|
83
|
+
q_minus_2, q_minus_1 = q_minus_1, q
|
84
|
+
end
|
85
|
+
|
86
|
+
ContinuedFraction.new((p_minus_1.to_r / q_minus_1).to_f, quotients.length)
|
87
|
+
end
|
88
|
+
|
74
89
|
def <=>(other)
|
75
90
|
number <=> other.number
|
76
91
|
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: 2.0
|
4
|
+
version: 2.1.0
|
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-12-
|
11
|
+
date: 2023-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -38,14 +38,25 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '11.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yard
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
41
55
|
description: Class for working with continued fractions
|
42
56
|
email: jose@halesgarcia.com
|
43
57
|
executables: []
|
44
58
|
extensions: []
|
45
|
-
extra_rdoc_files:
|
46
|
-
- LICENSE
|
47
|
-
- README.md
|
48
|
-
- lib/continued_fractions.rb
|
59
|
+
extra_rdoc_files: []
|
49
60
|
files:
|
50
61
|
- LICENSE
|
51
62
|
- README.md
|
@@ -56,13 +67,9 @@ licenses:
|
|
56
67
|
- MIT
|
57
68
|
metadata:
|
58
69
|
source_code_uri: https://github.com/jolohaga/continued_fractions
|
70
|
+
documentation_uri: https://jolohaga.github.io/continued_fractions
|
59
71
|
post_install_message:
|
60
|
-
rdoc_options:
|
61
|
-
- "--line-numbers"
|
62
|
-
- "--title"
|
63
|
-
- continued_fractions
|
64
|
-
- "--main"
|
65
|
-
- README.md
|
72
|
+
rdoc_options: []
|
66
73
|
require_paths:
|
67
74
|
- lib
|
68
75
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -74,9 +81,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
81
|
requirements:
|
75
82
|
- - ">="
|
76
83
|
- !ruby/object:Gem::Version
|
77
|
-
version: '1
|
84
|
+
version: '3.1'
|
78
85
|
requirements: []
|
79
|
-
rubygems_version: 3.
|
86
|
+
rubygems_version: 3.5.1
|
80
87
|
signing_key:
|
81
88
|
specification_version: 4
|
82
89
|
summary: Generate continued fractions
|