continued_fractions 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/Manifest +5 -0
- data/README.txt +70 -0
- data/Rakefile +13 -0
- data/continued_fractions.gemspec +30 -0
- data/lib/continued_fractions.rb +58 -0
- metadata +74 -0
data/History.txt
ADDED
data/Manifest
ADDED
data/README.txt
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
= ContinuedFractions
|
2
|
+
|
3
|
+
* FIX (url)
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Generates quotients and convergents for a given number.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Requires Ruby 1.9+
|
12
|
+
* Depends on Ruby 1.9's Rational class
|
13
|
+
|
14
|
+
== SYNOPSIS:
|
15
|
+
|
16
|
+
require 'continued_fractions'
|
17
|
+
include ContinuedFractions
|
18
|
+
|
19
|
+
frac = Rational(3,2)
|
20
|
+
real = Math.log2(ratio.to_f)
|
21
|
+
cf = ContinuedFraction.new(real,10)
|
22
|
+
=> #<ContinuedFractions::ContinuedFraction:0x000001009d1d00 @number=0.584962500721156, @limit=10, @quotients=[0, 1, 1, 2, 2, 3, 1, 5, 2, 23], @convergents=[[0, 1], [1, 0], [0, 1], [1, 1], [1, 2], [3, 5], [7, 12], [24, 41], [31, 53], [179, 306], [389, 665], [9126, 15601]]>
|
23
|
+
|
24
|
+
cf.convergents
|
25
|
+
=> [(0/1), (1/1), (1/2), (3/5), (7/12), (24/41), (31/53), (179/306), (389/665), (9126/15601)]
|
26
|
+
|
27
|
+
cf.convergent(4)
|
28
|
+
=> (7/12)
|
29
|
+
|
30
|
+
== REQUIREMENTS:
|
31
|
+
|
32
|
+
* Ruby 1.9+
|
33
|
+
|
34
|
+
== INSTALL:
|
35
|
+
|
36
|
+
sudo gem install continued_fractions
|
37
|
+
|
38
|
+
== DEVELOPERS:
|
39
|
+
|
40
|
+
After checking out the source, run:
|
41
|
+
|
42
|
+
$ rake newb
|
43
|
+
|
44
|
+
This task will install any missing dependencies, run the tests/specs,
|
45
|
+
and generate the RDoc.
|
46
|
+
|
47
|
+
== LICENSE:
|
48
|
+
|
49
|
+
(The MIT License)
|
50
|
+
|
51
|
+
Copyright (c) 2010 FIX
|
52
|
+
|
53
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
54
|
+
a copy of this software and associated documentation files (the
|
55
|
+
'Software'), to deal in the Software without restriction, including
|
56
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
57
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
58
|
+
permit persons to whom the Software is furnished to do so, subject to
|
59
|
+
the following conditions:
|
60
|
+
|
61
|
+
The above copyright notice and this permission notice shall be
|
62
|
+
included in all copies or substantial portions of the Software.
|
63
|
+
|
64
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
65
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
66
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
67
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
68
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
69
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
70
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('continued_fractions', '0.1.0') do |config|
|
6
|
+
config.description = 'Generate continued fractions.'
|
7
|
+
config.author = 'Jose Hales-Garcia'
|
8
|
+
config.email = 'jolohaga@me.com'
|
9
|
+
config.ignore_pattern = ["tmp/*",".hg/*"]
|
10
|
+
config.development_dependencies = []
|
11
|
+
end
|
12
|
+
|
13
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{|ext| load ext}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{continued_fractions}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Jose Hales-Garcia"]
|
9
|
+
s.date = %q{2010-03-14}
|
10
|
+
s.description = %q{Generate continued fractions.}
|
11
|
+
s.email = %q{jolohaga@me.com}
|
12
|
+
s.extra_rdoc_files = ["README.txt", "lib/continued_fractions.rb"]
|
13
|
+
s.files = ["History.txt", "Manifest", "README.txt", "Rakefile", "lib/continued_fractions.rb", "continued_fractions.gemspec"]
|
14
|
+
s.homepage = %q{}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Continued_fractions", "--main", "README.txt"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{continued_fractions}
|
18
|
+
s.rubygems_version = %q{1.3.6}
|
19
|
+
s.summary = %q{Generate continued fractions.}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module ContinuedFractions
|
2
|
+
# Requires Ruby 1.9
|
3
|
+
class ContinuedFraction
|
4
|
+
attr_accessor :number, :quotients, :limit
|
5
|
+
attr_writer :convergents
|
6
|
+
|
7
|
+
def initialize(n,l=5)
|
8
|
+
@number = n
|
9
|
+
@limit = l
|
10
|
+
@quotients = calculate_quotients
|
11
|
+
@convergents = calculate_convergents
|
12
|
+
end
|
13
|
+
|
14
|
+
def convergent(n)
|
15
|
+
convergents[n]
|
16
|
+
end
|
17
|
+
|
18
|
+
def convergents
|
19
|
+
_convergents.map{|c| Rational(c[0],c[1])}
|
20
|
+
end
|
21
|
+
|
22
|
+
def convergents_array
|
23
|
+
_convergents
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def _convergents
|
28
|
+
@convergents[2..@convergents.length]
|
29
|
+
end
|
30
|
+
|
31
|
+
def calculate_quotients
|
32
|
+
qs = Array.new(@limit)
|
33
|
+
n = @number
|
34
|
+
@limit.times do |i|
|
35
|
+
qs[i] = n.to_i
|
36
|
+
n = 1.0/(n-qs[i])
|
37
|
+
end
|
38
|
+
qs
|
39
|
+
end
|
40
|
+
|
41
|
+
def calculate_convergents
|
42
|
+
convs = ContinuedFractions.matrix(@limit+2,2,1)
|
43
|
+
convs[0][0] = 0
|
44
|
+
convs[1][1] = 0
|
45
|
+
2.upto(@limit+1) do |i|
|
46
|
+
convs[i][0] = convs[i-1][0]*quotients[i-2]+convs[i-2][0]
|
47
|
+
convs[i][1] = convs[i-1][1]*quotients[i-2]+convs[i-2][1]
|
48
|
+
end
|
49
|
+
convs
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class << self
|
54
|
+
def matrix(n,m,fill=nil)
|
55
|
+
Array.new(n).map!{Array.new(m,fill)}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: continued_fractions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jose Hales-Garcia
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-14 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Generate continued fractions.
|
22
|
+
email: jolohaga@me.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.txt
|
29
|
+
- lib/continued_fractions.rb
|
30
|
+
files:
|
31
|
+
- History.txt
|
32
|
+
- Manifest
|
33
|
+
- README.txt
|
34
|
+
- Rakefile
|
35
|
+
- lib/continued_fractions.rb
|
36
|
+
- continued_fractions.gemspec
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: ""
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --line-numbers
|
44
|
+
- --inline-source
|
45
|
+
- --title
|
46
|
+
- Continued_fractions
|
47
|
+
- --main
|
48
|
+
- README.txt
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 1
|
64
|
+
- 2
|
65
|
+
version: "1.2"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: continued_fractions
|
69
|
+
rubygems_version: 1.3.6
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Generate continued fractions.
|
73
|
+
test_files: []
|
74
|
+
|