Ad
Mathematics
Algorithms
Logic
Numbers

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)"

Code
Diff
  • 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_s
    • n[1] < 2 ? result += ')' : result += '**' + n[1].to_s + ')'
    • end
    • result
    • end
    • Prime.prime_division(n).map do |n|
    • "(#{n[0]}#{n[1] > 1 ? '**' + n[1].to_s : ''})"
    • end*""
    • end