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.
-
#phi ⇒ Integer
(also: #totient)
The count of coprimes less than self.
-
#prime_signature ⇒ Array
Of signature of self.
Instance Method Details
#coprime?(other) ⇒ Boolean
Returns if self is coprime with other number.
247 |
# File 'lib/tonal/extensions.rb', line 247 def coprime?(other) = self.gcd(other) == 1 |
#coprimes ⇒ Array
Returns list of integers that are coprime with self, up to the value of self.
253 254 255 256 257 258 259 |
# File 'lib/tonal/extensions.rb', line 253 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.
239 |
# File 'lib/tonal/extensions.rb', line 239 def factorial = (2..self).reduce(1, :*) |
#max_prime ⇒ Integer
Returns the maximum prime factor of self.
227 |
# File 'lib/tonal/extensions.rb', line 227 def max_prime = self.prime_division.map(&:first).max |
#min_prime ⇒ Integer
Returns the minimum prime factor of self.
233 |
# File 'lib/tonal/extensions.rb', line 233 def min_prime = self.prime_division.map(&:first).min |
#nsmooth(limit = 2) ⇒ Array
Note:
Adapted from rosettacode.org/wiki/N-smooth_numbers#Ruby
Returns of integers that are n-smooth with self.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/tonal/extensions.rb', line 275 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.
265 |
# File 'lib/tonal/extensions.rb', line 265 def phi = coprimes.count |
#prime_signature ⇒ Array
Returns of signature of self.
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/tonal/extensions.rb', line 299 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 |