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.
-
#ed(modulo, equave: 2) ⇒ Tonal::ReducedRatio
The ratio 2**(self/modulo).
-
#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.
-
#phi ⇒ Integer
(also: #totient)
The count of coprimes less than self.
-
#prime_signature ⇒ Array
Of signature of self.
-
#superparticular ⇒ Rational
The superparticular ratio based on the given integer as the numerator.
Instance Method Details
#coprime?(other) ⇒ Boolean
Returns if self is coprime with other number.
298 |
# File 'lib/tonal/extensions.rb', line 298 def coprime?(other) = self.gcd(other) == 1 |
#coprimes ⇒ Array
Returns list of integers that are coprime with self, up to the value of self.
304 305 306 307 308 309 310 |
# File 'lib/tonal/extensions.rb', line 304 def coprimes [].tap do |coprime_set| 1.upto(self) do |i| coprime_set << i if i.coprime?(self) end end end |
#ed(modulo, equave: 2) ⇒ Tonal::ReducedRatio
Returns the ratio 2**(self/modulo).
272 |
# File 'lib/tonal/extensions.rb', line 272 def ed(modulo, equave: 2/1r) = Tonal::ReducedRatio.ed(self, modulo, equave:) |
#factorial ⇒ Integer
Returns the factorial of self.
290 |
# File 'lib/tonal/extensions.rb', line 290 def factorial = (2..self).reduce(1, :*) |
#max_prime ⇒ Integer
Returns the maximum prime factor of self.
278 |
# File 'lib/tonal/extensions.rb', line 278 def max_prime = self.prime_division.map(&:first).max |
#min_prime ⇒ Integer
Returns the minimum prime factor of self.
284 |
# File 'lib/tonal/extensions.rb', line 284 def min_prime = self.prime_division.map(&:first).min |
#nsmooth(limit = 2) ⇒ Array
Adapted from rosettacode.org/wiki/N-smooth_numbers#Ruby
Returns of integers that are n-smooth with self.
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/tonal/extensions.rb', line 334 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.
324 |
# File 'lib/tonal/extensions.rb', line 324 def phi = coprimes.count |
#prime_signature ⇒ Array
Returns of signature of self.
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/tonal/extensions.rb', line 358 def prime_signature raise ArgumentError, "applicable only to positive integers" if self <= 0 n = self exponents = [] i = 2 while i * i <= n count = 0 while n % i == 0 n /= i count += 1 end exponents << count if count > 0 i += 1 end exponents << 1 if n > 1 # n is prime at this point exponents.sort end |
#superparticular ⇒ Rational
Returns the superparticular ratio based on the given integer as the numerator.
315 316 317 318 |
# File 'lib/tonal/extensions.rb', line 315 def superparticular return nil if self < 2 Rational(self, self-1) end |