Ad

Given a positive number n > 1 find the prime factor decomposition of n. The result will be a string with the following form :

"(p1n1)(p2n2)...(pk**nk)"
where a ** b means a to the power of b

with the p(i) in increasing order and n(i) empty if n(i) is 1.

Example: n = 86240 should return "(25)(5)(72)(11)"

require 'prime'

def primeFactors(n)
  result = ''
  Prime.prime_division(n).each do |n|
    result += '(' + n[0].to_s
    n[1] < 2 ? result += ')' : result += '**' + n[1].to_s + ')'
  end
  
  result
end