Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/tonal/extensions.rb

Instance Method Summary collapse

Instance Method Details

#coprime?(other) ⇒ Boolean

Returns if self is coprime with other number.

Examples:

25.coprime?(7) => true
25.coprime?(5) => false

Parameters:

  • other

    number determining coprimeness

Returns:

  • (Boolean)

    if self is coprime with other number



221
# File 'lib/tonal/extensions.rb', line 221

def coprime?(other) = self.gcd(other) == 1

#coprimesArray

Returns list of integers that are coprime with self, up to the value of self.

Examples:

10.coprimes => [1, 3, 7, 9]

Returns:

  • (Array)

    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

#factorialInteger

Returns the factorial of self.

Examples:

5.factorial => 120

Returns:

  • (Integer)

    the factorial of self



213
# File 'lib/tonal/extensions.rb', line 213

def factorial = (2..self).reduce(1, :*)

#max_primeInteger

Returns the maximum prime factor of self.

Examples:

72.max_prime => 3

Returns:

  • (Integer)

    the maximum prime factor of self



201
# File 'lib/tonal/extensions.rb', line 201

def max_prime = self.prime_division.map(&:first).max

#min_primeInteger

Returns the minimum prime factor of self.

Examples:

72.min_prime => 2

Returns:

  • (Integer)

    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.

Examples:

5.nsmooth(25)
=> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36, 40, 45, 48, 50, 54]

Parameters:

  • limit (defaults to: 2)

Returns:



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

#phiInteger Also known as: totient

Returns the count of coprimes less than self.

Examples:

10.phi => 4

Returns:

  • (Integer)

    the count of coprimes less than self



239
# File 'lib/tonal/extensions.rb', line 239

def phi = coprimes.count