erich-math 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/erichmath.gemspec +26 -0
- data/lib/erichmath/version.rb +3 -0
- data/lib/erichmath.rb +111 -0
- metadata +52 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/erichmath.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "erichmath/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "erich-math"
|
7
|
+
s.version = ErichMath::VERSION
|
8
|
+
s.authors = ["Erich Menge"]
|
9
|
+
s.email = ["erich.menge@me.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A simple educational math gem.}
|
12
|
+
s.description = %q{
|
13
|
+
This gem has a number of educational tools regarding math. Pairwise prime, factorial, euclidian gcd decomposition and more.
|
14
|
+
}
|
15
|
+
|
16
|
+
# s.rubyforge_project = "erich-math"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
# specify any dependencies here; for example:
|
24
|
+
# s.add_development_dependency "rspec"
|
25
|
+
# s.add_runtime_dependency "rest-client"
|
26
|
+
end
|
data/lib/erichmath.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require "erichmath/version"
|
2
|
+
require 'mathn'
|
3
|
+
|
4
|
+
module ErichMath
|
5
|
+
class SpecialInteger # Proxy class
|
6
|
+
instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval|object_id/ }
|
7
|
+
|
8
|
+
class << self
|
9
|
+
alias_method :[], :new
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(number)
|
13
|
+
@number = number
|
14
|
+
end
|
15
|
+
|
16
|
+
def number
|
17
|
+
@number
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_missing(meth, *args)
|
21
|
+
number.send(meth, *args)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Integer
|
28
|
+
include Math
|
29
|
+
|
30
|
+
class << self
|
31
|
+
def pairwise_prime?(*list)
|
32
|
+
raise ArgumentError, 'Need at least one number' if list.empty?
|
33
|
+
bad_pair = {}
|
34
|
+
pair_gcd = 1
|
35
|
+
list.combination(2).all? do |pair|
|
36
|
+
pair_gcd = pair[0].gcd(pair[1])
|
37
|
+
bad_pair = { pair: pair, gcd: pair_gcd } unless pair_gcd == 1
|
38
|
+
pair_gcd == 1
|
39
|
+
end
|
40
|
+
return pair_gcd == 1 ? true : bad_pair
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def factorial
|
45
|
+
raise RangeError, 'Argument must be a positive integer' if self < 0
|
46
|
+
(1..self).inject(1,:*)
|
47
|
+
end
|
48
|
+
|
49
|
+
def sqrt
|
50
|
+
super(self)
|
51
|
+
end
|
52
|
+
|
53
|
+
def primes(args = {})
|
54
|
+
args[:from] ||= 1
|
55
|
+
(args[:from]..self).find_all do |integer|
|
56
|
+
integer.prime?
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def factorize
|
61
|
+
factors = {}
|
62
|
+
class << factors
|
63
|
+
def to_s
|
64
|
+
string = ""
|
65
|
+
self.each do |index, value|
|
66
|
+
value > 1 ? string += "#{index}^#{value}*" : string += "#{index}*"
|
67
|
+
end
|
68
|
+
string.chomp("*")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
factors.default = 0
|
72
|
+
n = self
|
73
|
+
i = 2
|
74
|
+
sqi = 4
|
75
|
+
while sqi <= n do
|
76
|
+
while n.modulo(i) == 0 do
|
77
|
+
n /= i
|
78
|
+
factors[i] += 1
|
79
|
+
end
|
80
|
+
sqi += 2 * i + 1
|
81
|
+
i += 1
|
82
|
+
end
|
83
|
+
factors[n] += 1 if (n != 1) && (n != self)
|
84
|
+
factors
|
85
|
+
end
|
86
|
+
|
87
|
+
def euclidean_gcd(integer)
|
88
|
+
string = ""
|
89
|
+
x = self
|
90
|
+
y = integer
|
91
|
+
while y != 0
|
92
|
+
quotient, remainder = x.divmod(y)
|
93
|
+
string += "#{x} = #{quotient} * #{y} #{remainder > 0 ? "+ #{remainder}" : ''}\n"
|
94
|
+
x = y
|
95
|
+
y = remainder
|
96
|
+
end
|
97
|
+
x = ErichMath::SpecialInteger[x]
|
98
|
+
x.instance_eval %Q[
|
99
|
+
def to_s
|
100
|
+
"#{string}" << number.to_s
|
101
|
+
end
|
102
|
+
]
|
103
|
+
x
|
104
|
+
end
|
105
|
+
alias_method :e_gcd, :euclidean_gcd
|
106
|
+
|
107
|
+
def pairwise_prime?(*list)
|
108
|
+
self.class.pairwise_prime?(*(list << self))
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: erich-math
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Erich Menge
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-16 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! "\n This gem has a number of educational tools regarding math. Pairwise
|
15
|
+
prime, factorial, euclidian gcd decomposition and more.\n "
|
16
|
+
email:
|
17
|
+
- erich.menge@me.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- Rakefile
|
25
|
+
- erichmath.gemspec
|
26
|
+
- lib/erichmath.rb
|
27
|
+
- lib/erichmath/version.rb
|
28
|
+
homepage: ''
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.11
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: A simple educational math gem.
|
52
|
+
test_files: []
|