05.Primes in numbers

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

 "(p1**n1)(p2**n2)...(pk**nk)"

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

Example: n = 86240 should return "(2**5)(5)(7**2)(11)"

function primeFactors(n){ for (var i=2, res="", f; i <= n; i++) { f=0; while (n%i == 0) { f++; n/=i } res += f ? "(" + ( f>1 ? i+"**"+f : i ) +")" : "" } return res || "("+n+")" }//有待细读

posted on 2017-07-25 14:15  城南北  阅读(334)  评论(0)    收藏  举报

导航