Sha256: 139fedc48b8bc81e911568e43846951697848dd5d5eba843b0b54da8e1a79fc4

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

# coding: utf-8

class Integer
  def number_of_distinct_prime_factors
    prime_factors.uniq.size
  end
  alias :omega :number_of_distinct_prime_factors
  alias :ω :number_of_distinct_prime_factors

  def number_of_prime_factors
    prime_factors.size
  end
  alias :bigomega :number_of_prime_factors
  alias :Ω :number_of_prime_factors

  def prime_factors
    return [] if zero?
    prime_division.map{|pair| [pair.first] * pair.last}.flatten
  end

  def politeness
    divisors.select{|d| d > 1}.select{|d| d.odd?}.size
  end

  def proper_divisors
    divisors.reject {|d| d == self }
  end

  def divisors
    return [] unless positive?
    return [1, self] if prime?
    (1..Math.sqrt(self).floor).select { |n| (self % n).zero? }.
                               map {|n| [n, self/n]}.flatten.uniq
  end

  def sum_of_divisors
    @sod ||= divisors.reduce(:+)
  end
  alias :σ :sum_of_divisors

  def aliquot_sum
    return 0 if zero?
    σ - self
  end

  def digital_root
    self == 0 ? 0 : 1 + ((self - 1) % 9)
  end

  def digital_sum
    digits.reduce(:+)
  end
  alias :sum_of_digits :digital_sum
  alias :sod :digital_sum

  def digits
    self.to_s.split(//).map{|d| d.to_i}
  end

  def divides?(n)
    not n.zero? and (self % n).zero?
  end
end

require 'prime'

Dir.glob(File.dirname(__FILE__) + '/numb/*.rb').each {|file| require file}

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
numb-0.114.0 lib/numb.rb
numb-0.111.0 lib/numb.rb
numb-0.109.0 lib/numb.rb
numb-0.99.0 lib/numb.rb
numb-0.96.0 lib/numb.rb
numb-0.89.0 lib/numb.rb
numb-0.84.0 lib/numb.rb