open_integer 0.4.0.pre.46

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/open_integer.rb +232 -0
  3. metadata +100 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzcxM2RkMWZkNzA5YmNhZGVhNDI2ZDI5ZmZjOWY1YTc4OTY5YTVkOA==
5
+ data.tar.gz: !binary |-
6
+ ZTI5NTBhMzJiYWU3ODZhNDY1NzQwOTlhYmVhYjkzMTYyYzVhNmYwNw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjM1MGRlMzAwOTc4N2U0ZGQyMzY1NWU3Njg4YmNmZTY0ZGUxN2ExYzg0YWEw
10
+ MWZiN2IwY2Y3YjFmZTc4NTA2NDgxYzc2N2EzYTU2MTViMGY0MmMwMGRkZGUx
11
+ YTBkM2RmYTYzNDBhNzkwMzljZTUzMmNjMjc2MGI1MDEwMTBhN2M=
12
+ data.tar.gz: !binary |-
13
+ YzBmMDZlYmNhN2Q1MDNhZDdhZDE4ZDk5ZmE4NDg4MGM4NzY4ODBjMWNlN2Fh
14
+ MTk4YzA1YzdhNmQ0MmE3YjQ4YzEwN2E4MDk5YjMwMWJlMmU0ZmEzNTQwNmI5
15
+ Njc2MDNiMDE5NmNiZWU5MDVlNWFjNjc3MWZjMzA4MjQ0OTVhN2M=
@@ -0,0 +1,232 @@
1
+ require 'prime'
2
+
3
+ # Public: Provides extensions to the built-in Integer class.
4
+ module OpenInteger
5
+ # Public: Determines if the receiver is an abundant number. A number is
6
+ # abundant iff the sum of its proper divisors is greater than the number
7
+ # itself.
8
+ #
9
+ # Returns true iff the sum of the proper divisors of the receiver is greater
10
+ # than the receiver.
11
+ def abundant?
12
+ divisors.reduce(:+) > self
13
+ end
14
+
15
+ # Public: Determines if the receiver and other are an amicable pair. Numbers a
16
+ # and b are amicable if the sum of the factors of a equals b, the sum of the
17
+ # factors of b equals a, and a is not equal to b.
18
+ #
19
+ # other - The value which may be amicable with the receiver.
20
+ #
21
+ # Returns true iff the receiver is amicable with other.
22
+ def amicable_with?(other)
23
+ factors.reduce(:+) == other &&
24
+ other.factors.reduce(:+) == self &&
25
+ self != other
26
+ end
27
+
28
+ # Public: An implementation of n choose k using factorials. This
29
+ # implementation works for all n >= k >= 0
30
+ #
31
+ # k - The k parameter of the n choose k formula.
32
+ #
33
+ # Returns the result of n choose k where n is the receiver and k is the
34
+ # Integer argument
35
+ def choose(k)
36
+ fail 'This implementation of n choose k only works for n >= k >= 0.' unless
37
+ 0 <= k && k <= self
38
+ choose_factorial k
39
+ end
40
+
41
+ # Protected: An implementation of n choose k using factorials which works for
42
+ # values of n >= k >= 0.
43
+ #
44
+ # k - The k parameter of the n choose k formula.
45
+ #
46
+ # Returns the result of n choose k using the multiplicative impelementation.
47
+ def choose_factorial(k)
48
+ return 1 if 0 == k && k <= self
49
+ n_factorial = factorial
50
+ k_factorial = k.factorial
51
+ n_minus_k_factorial = (self - k).factorial
52
+ n_factorial / (k_factorial * n_minus_k_factorial)
53
+ end
54
+
55
+ # Public: Generates the Collatz sequence beginning with the receiver.
56
+ #
57
+ # Returns an enumerator if no block is given.
58
+ #
59
+ # Yields each value in the sequence as it is calculated.
60
+ def collatz_sequence
61
+ return to_enum(:collatz_sequence) unless block_given?
62
+ yield val = self
63
+ yield val = val.next_in_collatz_sequence until val == 1
64
+ end
65
+
66
+ # Deprecated: Determines the number of factors for the receiver.
67
+ #
68
+ # This is deprecated in favor of calling n.factors.count.
69
+ #
70
+ # Returns the number of factors the receiver has.
71
+ def count_factors
72
+ factors.count
73
+ end
74
+
75
+ # Public: Determines if the receiver is a deficient number. A number is
76
+ # deficient iff the sum of its proper divisors is less than the number itself.
77
+ #
78
+ # Returns true iff the sum of the proper divisors of the receiver is less than
79
+ # the receiver.
80
+ def deficient?
81
+ divisors.reduce(:+) < self
82
+ end
83
+
84
+ # Public: Finds all proper divisors of the receiver. Proper divisors of a
85
+ # number n are positive divisors of n excluding n itself.
86
+ #
87
+ # See the definition at Wolfram MathWorld for more details.
88
+ # http://mathworld.wolfram.com/ProperDivisor.html
89
+ #
90
+ # Returns the proper divisors of the receiver.
91
+ # Raises RuntimeError if called on an Integer less than 1.
92
+ #
93
+ # Yields each proper divisors as it is identified.
94
+ def divisors
95
+ fail 'Integers less than 1 do not have proper divisors.' if self < 1
96
+ return to_enum(:divisors) unless block_given?
97
+ top = self == 1 ? 1 : (self / 2).to_i
98
+
99
+ (1..top).each do |num|
100
+ yield num if num.factor_of? self
101
+ end
102
+ end
103
+
104
+ # Public: Determines if the receiver is a factor of other (i.e. if other is
105
+ # evenly divisible by the receiver).
106
+ #
107
+ # other - The Integer which may be a factor of the receiver.
108
+ #
109
+ # Returns true iff the receiver is a factor of other.
110
+ # Raises RuntimeError if called on 0.
111
+ def factor_of?(other)
112
+ fail 'Zero is not a factor of anything.' if self == 0
113
+ !other.nil? && other % self == 0
114
+ end
115
+
116
+ # Public: Finds factor pairs of the receiver.
117
+ #
118
+ # Returns the factor pairs of the receiver.
119
+ #
120
+ # Yields each factor pair as it is identified.
121
+ def factor_pairs
122
+ return to_enum(:factor_pairs) unless block_given?
123
+
124
+ factors.each do |factor|
125
+ yield [factor, self / factor]
126
+ end
127
+ end
128
+
129
+ # Public: Calculates the factorial of the receiver.
130
+ #
131
+ # Returns the factorial of the receiver.
132
+ def factorial
133
+ return 1 if self == 0
134
+ (1..self).inject(:*)
135
+ end
136
+
137
+ # Public: Finds all proper factors of the receiver. A proper factor of a
138
+ # positive integer n is a factor of n other than 1 or n.
139
+ #
140
+ # See the definition at Wolfram MathWorld for more details.
141
+ # http://mathworld.wolfram.com/ProperFactor.html
142
+ #
143
+ # Returns the proper factors of the receiver.
144
+ # Raises a RuntimeError if called on an Integer less than 2.
145
+ #
146
+ # Yields each proper factor as it is identified.
147
+ def factors
148
+ fail 'Integers less than 2 do not have proper factors.' if self < 2
149
+ return to_enum(:factors) unless block_given?
150
+ top = (self / 2).to_i
151
+
152
+ (2..top).each do |num|
153
+ yield num if num.factor_of? self
154
+ end
155
+ end
156
+
157
+ # Public: Finds the lowest prime factor of the receiver.
158
+ #
159
+ # Returns the lowest prime factor of the receiver or nil if the receiver has
160
+ # no prime factors.
161
+ def lowest_prime_factor
162
+ top = (Math.sqrt self).to_i
163
+
164
+ Prime.each top do |prime|
165
+ return prime if prime.factor_of? self
166
+ end
167
+ end
168
+
169
+ # Public: Finds the next number in the Collatz sequence using the receiver as
170
+ # the initial value. The Collatz sequence can be generated for successive
171
+ # values n by applying n / 2 if n is even or n * 3 + 1 if n is odd.
172
+ #
173
+ # Returns the next value in the Collatz sequence.
174
+ def next_in_collatz_sequence
175
+ coll = self / 2 if even?
176
+ coll = (self * 3) + 1 if odd?
177
+ coll
178
+ end
179
+
180
+ # Public: Finds the nth triangular number. The nth triangular number is
181
+ # generated by adding natural numbers up to n. For n = 7, the triangular
182
+ # number is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.
183
+ #
184
+ # Returns the nth triangular number.
185
+ def self.nth_triangular_number(n)
186
+ (1..n).reduce(:+)
187
+ end
188
+
189
+ # Public: Determines if the receiver is a perfect number. A number is perfect
190
+ # iff the sum of its proper divisors is exactly equal to the number itself.
191
+ #
192
+ # Returns true iff the receiver is equal to the sum of its proper divisors.
193
+ def perfect?
194
+ divisors.reduce(:+) == self
195
+ end
196
+
197
+ # Public: Determines the prime factors of the receiver.
198
+ #
199
+ # Returns an Enumerator over the collection of the receiver's prime factors.
200
+ #
201
+ # Yields each prime factor as it is discovered.
202
+ def prime_factors
203
+ return to_enum(:prime_factors) unless block_given?
204
+ top = (Math.sqrt self).to_i
205
+
206
+ Prime.each top do |prime|
207
+ yield prime if prime.factor_of? self
208
+ end
209
+ end
210
+
211
+ # Public: Asks the receiver if it has any prime factors.
212
+ #
213
+ # Returns true iff the receiver has any prime factors else returns false.
214
+ def prime_factors?
215
+ !lowest_prime_factor.nil?
216
+ end
217
+
218
+ # Public: Determines if the three Integer arguments are a Pythagorean triplet
219
+ # of the form a^2 + b^2 = c^2.
220
+ #
221
+ # Returns true iff the arguments follow the form a^2 + b^2 = c^2.
222
+ def self.pythagorean_triplet?(a, b, c)
223
+ (a * a) + (b * b) == (c * c)
224
+ end
225
+
226
+ private :choose_factorial
227
+ end
228
+
229
+ # Public: Mixing OpenInteger extensions into Integer.
230
+ class Integer
231
+ include OpenInteger
232
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_integer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0.pre.46
5
+ platform: ruby
6
+ authors:
7
+ - Jason Conner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simplecov
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fivemat
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ description: Various extensions for the Integer class
70
+ email: jason.r.conner@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/open_integer.rb
76
+ homepage: https://github.com/jrconner384/open_integer
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: 1.9.2
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ! '>'
92
+ - !ruby/object:Gem::Version
93
+ version: 1.3.1
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: open_integer
100
+ test_files: []