dgoldhirsch-cs 0.0.0
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.
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/lib/cs.rb +74 -0
- data/test/cs_test.rb +57 -0
- data/test/test_helper.rb +9 -0
- metadata +57 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 dgoldhirsch
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/lib/cs.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
# Title:: Computer Science Library for Ruby
|
4
|
+
# Author:: David C. Goldhirsch
|
5
|
+
# Copyright (c) 2009, David C. Goldhirsch
|
6
|
+
class CS
|
7
|
+
# Fibonacci number generator.
|
8
|
+
# Return integer value of F(n) for any integer n >= 1,
|
9
|
+
# where F(n) is defined as F(n - 1) + F(n - 2) for n > 2, F(2) = 1
|
10
|
+
# and F(1) = 0.
|
11
|
+
#
|
12
|
+
# E.g., the sequence (F(1), ..., F(7)) is (0, 1, 1, 2, 3, 5, 8).
|
13
|
+
#
|
14
|
+
# Values of n less than 1 are equivalent to n = 1.
|
15
|
+
def self.fibonacci n
|
16
|
+
return 0 if n <= 1
|
17
|
+
return (FibonacciSecondOrHigher.advanced_by n - 2).result
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# The class 'new' method is privatized, because users should not be
|
23
|
+
# instantiating this class.
|
24
|
+
def self.new
|
25
|
+
super.new
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Fibonacci generating class, each of whose instances represents F(2 + k)
|
32
|
+
# for some given k. To obtain the integer value of F(w), use the 'result' method, e.g.:
|
33
|
+
# - f = FibonacciSecondOrHigher.new
|
34
|
+
# - f.result # the integer 1 = F(2)
|
35
|
+
# - f.advance
|
36
|
+
# - f.result # the integer 2 = F(3)
|
37
|
+
# - etc.
|
38
|
+
class FibonacciSecondOrHigher
|
39
|
+
attr_accessor :result
|
40
|
+
attr_accessor :previous
|
41
|
+
attr_accessor :second_previous # non-nil only for F(3) and higher
|
42
|
+
|
43
|
+
# This is the proper way to create an instance
|
44
|
+
# representing F(2 + k). Please use this instead of 'new'.
|
45
|
+
# Return an instance representing F(2 + k) for given k.
|
46
|
+
# E.g., if k is 0, the result is F(2); if k is 1, the
|
47
|
+
# result is F(3); if k is 4 the result is F(6)
|
48
|
+
def self.advanced_by k
|
49
|
+
return (1..k).inject(FibonacciSecondOrHigher.new) do |f, i|
|
50
|
+
f.advance
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Change internal state such that if the receiver currently
|
55
|
+
# represents F(k) it will be advanced to represent F(k + 1).
|
56
|
+
def advance
|
57
|
+
self.second_previous = self.previous
|
58
|
+
self.previous = self.result
|
59
|
+
self.result += self.second_previous # i.e., second_previous + previous
|
60
|
+
return self
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_s
|
64
|
+
return second_previous.to_s + "," + previous.to_s + "," + "=>" + result.to_s
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
# Instances represent F(2), unless/until they are advanced
|
70
|
+
def initialize
|
71
|
+
self.previous = 0 # F(<= 1)
|
72
|
+
self.result = 1 # F(2)
|
73
|
+
end
|
74
|
+
end
|
data/test/cs_test.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class FibonacciTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
# Basic test of CS::fibonacci. Verify correctness of F(1, ...)
|
6
|
+
# as (0, 1, 1, 2, 3, 5, 8...).
|
7
|
+
def test_basic
|
8
|
+
assert_equal 0, CS::fibonacci(1)
|
9
|
+
assert_equal 1, CS::fibonacci(2)
|
10
|
+
assert_equal 1, CS::fibonacci(3)
|
11
|
+
assert_equal 2, CS::fibonacci(4)
|
12
|
+
assert_equal 3, CS::fibonacci(5)
|
13
|
+
assert_equal 5, CS::fibonacci(6)
|
14
|
+
assert_equal 8, CS::fibonacci(7)
|
15
|
+
end
|
16
|
+
|
17
|
+
# The assertion in this test is meaningless. The point of running this test
|
18
|
+
# is to be sure that Fibonacci numbers up to some large number can be computed
|
19
|
+
# within a reasonable CPU/memory footprint. For example, if a recursive rather
|
20
|
+
# than a linear algorithm were used, this test case would require 2**10,000 recursive
|
21
|
+
# calls, and the test would probably run out of memory.
|
22
|
+
def test_raw_performance
|
23
|
+
x = CS::fibonacci(10000);
|
24
|
+
assert x > 0
|
25
|
+
end
|
26
|
+
|
27
|
+
#The following function will take a massive amount of CPU time and an impossible amount of memory,
|
28
|
+
#because it uses the 2**n recursive algorithm to compute Fibonacci number n. If you want
|
29
|
+
#proof that the recursive algorithm is intractible, rename this function 'test_something', and
|
30
|
+
#watch your test runner run out of memory (slowly......)
|
31
|
+
def zzz_test_recursive # rename test_recursive, if you want to run it
|
32
|
+
assert_equal 0, recursive_fibonacci(1)
|
33
|
+
assert_equal 1, recursive_fibonacci(2)
|
34
|
+
assert_equal 1, recursive_fibonacci(3)
|
35
|
+
assert_equal 2, recursive_fibonacci(4)
|
36
|
+
assert_equal 3, recursive_fibonacci(5)
|
37
|
+
assert_equal 5, recursive_fibonacci(6)
|
38
|
+
assert_equal 8, recursive_fibonacci(7)
|
39
|
+
x = recursive_fibonacci(1000); # gonna take a loooooonnnnnngggggg time.....
|
40
|
+
assert x > 0
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
# Seemingly innocent, recursive algorithm that computes the nth Fibonacci number.
|
46
|
+
# Requires 2**n recursive calls, requiring an obscene amount of memory and CPU time.
|
47
|
+
# Try it for n = 1000, and then twiddle your thumbs while waiting for the "out of memory"
|
48
|
+
# exception...
|
49
|
+
def recursive_fibonacci(n)
|
50
|
+
if(n == 1)
|
51
|
+
return 0
|
52
|
+
elsif(n == 2 || n == 3)
|
53
|
+
return 1
|
54
|
+
end
|
55
|
+
return recursive_fibonacci(n - 2) + recursive_fibonacci(n - 1)
|
56
|
+
end
|
57
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dgoldhirsch-cs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- dgoldhirsch
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-06 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: dgoldhirsch@yahoo.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- lib/cs.rb
|
27
|
+
- LICENSE
|
28
|
+
- README.rdoc
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://github.com/dgoldhirsch/cs
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --charset=UTF-8
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.2.0
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: Computer Science Library for Ruby
|
55
|
+
test_files:
|
56
|
+
- test/cs_test.rb
|
57
|
+
- test/test_helper.rb
|