open_integer 0.4.0.pre.46 → 0.4.0.pre.48
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/open_integer.rb +55 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZWY1OWIzZWFlN2QwMjBlNDZmY2EyMGFmNjQwODE4N2JjYjYwZTg3OQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NmQ4MWU4MThjNzg3NGVmZjViNDFlOWJlOTBhM2E5OWU3MGUwMWQzZQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YWY1NTI4NzEyNzk5NzdiNjE3NjgwN2RjYzg4NDdmYjdlMWM4NWMzODI1ZDg0
|
10
|
+
NGYwZjdmYWI0ZjA3ZjdjNDQ4MzFkOWRjYjJmYjQ3ODFmYTdjODZmMzA3YzFl
|
11
|
+
NjMwMzI1YjNmZTFhYzEyZmRmZTA3ZTFkNWRhNzlhY2UzZGM2NTA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
M2NjNjEyYTZjODVlMDNiM2UwZjJhMzE4NDMyN2EyMjllZTdiMzkwYmU0ZTA5
|
14
|
+
ZDBiMDI3YzliZjA1YjljYTJkMDk3ZTkwZTk0MGQ1NDNmYmRhYTJmNWNiMWZk
|
15
|
+
NzIwYjE3NzEzMDJmYTExMzlkY2E2NWNjYWIyNDFkZWY0ZTZlNDU=
|
data/lib/open_integer.rb
CHANGED
@@ -13,18 +13,30 @@ module OpenInteger
|
|
13
13
|
end
|
14
14
|
|
15
15
|
# Public: Determines if the receiver and other are an amicable pair. Numbers a
|
16
|
-
# and b are amicable if the sum of the
|
17
|
-
#
|
16
|
+
# and b are amicable if the sum of the divisors of a equals b, the sum of the
|
17
|
+
# divisors of b equals a, and a is not equal to b.
|
18
18
|
#
|
19
19
|
# other - The value which may be amicable with the receiver.
|
20
20
|
#
|
21
21
|
# Returns true iff the receiver is amicable with other.
|
22
22
|
def amicable_with?(other)
|
23
|
-
|
24
|
-
other.
|
23
|
+
divisors.reduce(:+) == other &&
|
24
|
+
other.divisors.reduce(:+) == self &&
|
25
25
|
self != other
|
26
26
|
end
|
27
27
|
|
28
|
+
# Public: Determines if the receiver is amicable with any of the arguments.
|
29
|
+
#
|
30
|
+
# others - One or more values to test for amicability with the receiver.
|
31
|
+
#
|
32
|
+
# Returns true iff the receiver is amicable with any of the others.
|
33
|
+
def amicable_with_any?(*others)
|
34
|
+
others.each do |other|
|
35
|
+
return true if amicable_with? other
|
36
|
+
end
|
37
|
+
false
|
38
|
+
end
|
39
|
+
|
28
40
|
# Public: An implementation of n choose k using factorials. This
|
29
41
|
# implementation works for all n >= k >= 0
|
30
42
|
#
|
@@ -154,6 +166,44 @@ module OpenInteger
|
|
154
166
|
end
|
155
167
|
end
|
156
168
|
|
169
|
+
# Public: Returns the nth Fibonacci number where n is the receiver.
|
170
|
+
#
|
171
|
+
# Examples
|
172
|
+
#
|
173
|
+
# 1.fibonacci
|
174
|
+
# # => 1
|
175
|
+
#
|
176
|
+
# 2.fibonacci
|
177
|
+
# # => 1
|
178
|
+
#
|
179
|
+
# 3.fibonacci
|
180
|
+
# # => 2
|
181
|
+
#
|
182
|
+
# 3.fibonacci + 1.fibonacci
|
183
|
+
# # => 3
|
184
|
+
#
|
185
|
+
# Returns the nth Fibonacci number where n is the receiver.
|
186
|
+
# Raises a RuntimeError if called on any Integer less than 1.
|
187
|
+
def fibonacci
|
188
|
+
return 0 if self <= 0
|
189
|
+
fibonacci_fast_double(self - 1)[1]
|
190
|
+
end
|
191
|
+
|
192
|
+
# Internal: This is the fast-doubling Fibonacci algorithm helper which is
|
193
|
+
# meant to be used by the publicly exposed fibonacci method.
|
194
|
+
#
|
195
|
+
# Returns a two-member array of the form [F(n), F(n+1)]
|
196
|
+
def fibonacci_fast_double(n)
|
197
|
+
return [0, 1] if n <= 0
|
198
|
+
ab = fibonacci_fast_double(n / 2)
|
199
|
+
a = ab[0]
|
200
|
+
b = ab[1]
|
201
|
+
c = a * (2 * b - a)
|
202
|
+
d = b * b + a * a
|
203
|
+
return [c, d] if n.even?
|
204
|
+
[d, c + d]
|
205
|
+
end
|
206
|
+
|
157
207
|
# Public: Finds the lowest prime factor of the receiver.
|
158
208
|
#
|
159
209
|
# Returns the lowest prime factor of the receiver or nil if the receiver has
|
@@ -223,7 +273,7 @@ module OpenInteger
|
|
223
273
|
(a * a) + (b * b) == (c * c)
|
224
274
|
end
|
225
275
|
|
226
|
-
private :choose_factorial
|
276
|
+
private :choose_factorial, :fibonacci_fast_double
|
227
277
|
end
|
228
278
|
|
229
279
|
# Public: Mixing OpenInteger extensions into Integer.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_integer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.0.pre.
|
4
|
+
version: 0.4.0.pre.48
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Conner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|