Class: Integer
- Inherits:
-
Object
- Object
- Integer
- Defined in:
- lib/tonal/extensions.rb
Instance Method Summary collapse
-
#coprime?(other) ⇒ Boolean
If self is coprime with other number.
-
#coprimes ⇒ Array
List of integers that are coprime with self, up to the value of self.
-
#factorial ⇒ Integer
The factorial of self.
-
#max_prime ⇒ Integer
The maximum prime factor of self.
-
#min_prime ⇒ Integer
The minimum prime factor of self.
-
#nsmooth(limit = 2) ⇒ Array
Of integers that are n-smooth with self Adapted from rosettacode.org/wiki/N-smooth_numbers#Ruby.
-
#phi ⇒ Integer
(also: #totient)
The count of coprimes less than self.
Instance Method Details
#coprime?(other) ⇒ Boolean
Returns if self is coprime with other number.
221 |
# File 'lib/tonal/extensions.rb', line 221 def coprime?(other) = self.gcd(other) == 1 |
#coprimes ⇒ Array
Returns list of integers that are coprime with self, up to the value of self.
227 228 229 230 231 232 233 |
# File 'lib/tonal/extensions.rb', line 227 def coprimes [].tap do |coprime_set| 1.upto(self) do |i| coprime_set << i if i.coprime?(self) end end end |
#factorial ⇒ Integer
Returns the factorial of self.
213 |
# File 'lib/tonal/extensions.rb', line 213 def factorial = (2..self).reduce(1, :*) |
#max_prime ⇒ Integer
Returns the maximum prime factor of self.
201 |
# File 'lib/tonal/extensions.rb', line 201 def max_prime = self.prime_division.map(&:first).max |
#min_prime ⇒ Integer
Returns the minimum prime factor of self.
207 |
# File 'lib/tonal/extensions.rb', line 207 def min_prime = self.prime_division.map(&:first).min |
#nsmooth(limit = 2) ⇒ Array
Returns of integers that are n-smooth with self Adapted from rosettacode.org/wiki/N-smooth_numbers#Ruby.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/tonal/extensions.rb', line 249 def nsmooth(limit=2) ([0] * limit).tap do |ns| primes = Prime.each(self).to_a ns[0] = 1 nextp = primes[0..primes.index(self)] indices = [0] * nextp.size (1...limit).each do |m| ns[m] = nextp.min (0...indices.size).each do |i| if ns[m] == nextp[i] indices[i] += 1 nextp[i] = primes[i] * ns[indices[i]] end end end end end |
#phi ⇒ Integer Also known as: totient
Returns the count of coprimes less than self.
239 |
# File 'lib/tonal/extensions.rb', line 239 def phi = coprimes.count |