prime_factors.rb

1
2
3
4
5
6
7
8
class PrimeFactors
  def self.for(n)
    return [] if n == 1

    p = (2..n).detect { |x| n % x == 0 }
    [p] + self.for(n / p)
  end
end

@remcopeereboom thinks this looks great

Comments

Love the recursion!

remcopeereboom commented 26 April 2016 at 16:58 UTC

You're not logged in right now. Please login via GitHub to comment