re: Daily Challenge #7 - Factorial Decomposition VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Woo 3 days in a row! I'm not too good when it comes to maths and I'm sure there are loads of more efficient algorithms for finding prime factors, ...
 

Turns out there's actually a built in method for this ... So just the formatting I guess...

require 'prime'
def decomp n
  fact = Math.gamma(n + 1).to_i
  "n = #{n}; n! = #{fact}; decomp(n!) = #{n == 1 ? "1" : Prime.prime_division(fact).map(&proc {|pair| pair[1] > 1 ? "#{pair[0]}^#{pair[1]}" : pair[0]}).join(" * ")}"
end
(1..10).each {|x| puts decomp x}
code of conduct - report abuse