continued_fractions 2.0.3 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -3
  3. data/lib/continued_fractions.rb +24 -9
  4. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ffe169ac2c266d57c725c7991888a41efbb5bc8696bc6e08690fb71da0bf3d45
4
- data.tar.gz: 45cece67fcee46aaf95dcd55f6f84a5883d165e1b595a6a09e4a8ee352da492c
3
+ metadata.gz: 0dc6cd2a45f726fa6342ebf0efb882e1987e809bdf5527423e824dff1d20e4c2
4
+ data.tar.gz: c9c654e3bc9de045ceab233afabd54c716e576e968edbaddd020c225104efda6
5
5
  SHA512:
6
- metadata.gz: e96bd62ad78689b3232bc0d5f56de48227ddfcaaea7d09773bcd54625919e1ccffc8586bfda2cc1aa93c0eaa6f5e1dedf486233cc42981b89a5459c4c2264560
7
- data.tar.gz: 23bc9894cef7befca7e73b7b78e08eb1ca6cf71f382e8706de8fa571dee9f06dfd657b1b8b68ffd74651779ccc30c0a1d412906f0d491c0a1dfef237e0028074
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
 
@@ -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.3
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-06 00:00:00.000000000 Z
11
+ date: 2023-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -67,6 +67,7 @@ licenses:
67
67
  - MIT
68
68
  metadata:
69
69
  source_code_uri: https://github.com/jolohaga/continued_fractions
70
+ documentation_uri: https://jolohaga.github.io/continued_fractions
70
71
  post_install_message:
71
72
  rdoc_options: []
72
73
  require_paths:
@@ -82,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
83
  - !ruby/object:Gem::Version
83
84
  version: '3.1'
84
85
  requirements: []
85
- rubygems_version: 3.4.22
86
+ rubygems_version: 3.5.1
86
87
  signing_key:
87
88
  specification_version: 4
88
89
  summary: Generate continued fractions