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.
-
#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.
290 |
# File 'lib/tonal/extensions.rb', line 290 def coprime?(other) = self.gcd(other) == 1 |
#coprimes ⇒ Array
Returns list of integers that are coprime with self, up to the value of self.
296 297 298 299 300 301 302 |
# File 'lib/tonal/extensions.rb', line 296 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).
276 |
# File 'lib/tonal/extensions.rb', line 276 def ed(modulo, equave: 2/1r) = Tonal::ReducedRatio.ed(self, modulo, equave:) |
#factorial ⇒ Integer
Returns the factorial of self.
282 |
# File 'lib/tonal/extensions.rb', line 282 def factorial = (2..self).reduce(1, :*) |
#nsmooth(limit = 2) ⇒ Array
Adapted from https://rosettacode.org/wiki/N-smooth_numbers#Ruby
Returns of integers that are n-smooth with self.
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/tonal/extensions.rb', line 326 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.
316 |
# File 'lib/tonal/extensions.rb', line 316 def phi = coprimes.count |
#prime_signature ⇒ Array
Returns of signature of self.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/tonal/extensions.rb', line 350 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.
307 308 309 310 |
# File 'lib/tonal/extensions.rb', line 307 def superparticular return nil if self < 2 Rational(self, self-1) end |