题解 | #质数因子#
质数因子
http://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
const num = +readline()
const cache = []
getFactor(num, cache)
print(cache.sort((a, b)=>a-b).join(' '))
function getFactor(num, cache){
if(num === 1) return
if(isEven(num)){
cache.push(2)
return getFactor(num/2, cache)
}
if(isFifth(num)){
cache.push(5)
return getFactor(num/5, cache)
}
if(isTriple(num)){
cache.push(3)
return getFactor(num/3, cache)
}
normalize(7, num, cache)
}
function isEven(num){
return (num & 1) === 0
}
function isTriple(num){
return String(num).split('').reduce((prev, cur) => +prev + +cur) % 3 === 0
}
function isFifth(num){
return String(num).slice(-1) === '5'
}
function normalize(tmp, num, cache){
if(divideless(num)){
return cache.push(num)
}
if(tmp * tmp <= num ){
if(num % tmp === 0){
cache.push(tmp)
return normalize(7, num / tmp, cache)
}
normalize(tmp+1, num, cache)
}else{
cache.push(num)
}
}
function divideless(num){
const n = String(num).slice(-1)
return n === '3' || n === '7'
}