module RFactor::Factor

Public Class Methods

get_factors(n) click to toggle source
# File lib/r_factor/factor.rb, line 3
def self.get_factors(n)
  factors = []
  while n >= 2
    new_factor = get_smallest_factor(n)
    n /= new_factor
    factors << new_factor
  end
  factors
end

Private Class Methods

get_smallest_factor(n) click to toggle source
# File lib/r_factor/factor.rb, line 15
def self.get_smallest_factor(n)
  return 2 if n.even?
  3.step(Math.sqrt(n).floor, 2) do |candidate|
    return candidate if n % candidate == 0
  end
  n
end