Given a positive number n > 1 find the prime factor decomposition of n. Format the result as a string in the following form :
"(p_1**n_1)(p_2**n_2)...(p_k**n_k)"
where a ** b means a to the power of b
with the p_i in increasing order without the "**n_i".
Examples:
n = 17 should return "(17)"
n = 10976 should return "(2**5)(7**3)"
n = 86240 should return "(2**5)(5)(7**2)(11)"
require 'prime' def primeFactors(n) Prime.prime_division(n).map do |n| "(#{n[0]}#{n[1] > 1 ? '**' + n[1].to_s : ''})" end*"" end
- require 'prime'
- def primeFactors(n)
result = ''Prime.prime_division(n).each do |n|result += '(' + n[0].to_sn[1] < 2 ? result += ')' : result += '**' + n[1].to_s + ')'endresultend- Prime.prime_division(n).map do |n|
- "(#{n[0]}#{n[1] > 1 ? '**' + n[1].to_s : ''})"
- end*""
- end
Test.assert_equals(primeFactors(7), "(7)") Test.assert_equals(primeFactors(343), "(7**3)") Test.assert_equals(primeFactors(10976), "(2**5)(7**3)") Test.assert_equals(primeFactors(7775460), "(2**2)(3**3)(5)(7)(11**2)(17)") Test.assert_equals(primeFactors(9484985), "(5)(193)(9829)") Test.assert_equals(primeFactors(16516513), "(13)(359)(3539)")
- Test.assert_equals(primeFactors(7), "(7)")
- Test.assert_equals(primeFactors(343), "(7**3)")
- Test.assert_equals(primeFactors(10976), "(2**5)(7**3)")
- Test.assert_equals(primeFactors(7775460), "(2**2)(3**3)(5)(7)(11**2)(17)")
- Test.assert_equals(primeFactors(9484985), "(5)(193)(9829)")
- Test.assert_equals(primeFactors(16516513), "(13)(359)(3539)")