constdecomp=number=>{// function that adds the dividers of a number to a "dividers object"constsubdecomp=(number,subdividers)=>{letremainder=number// from 2 to square root of the numberfor(x=2;x<=Math.sqrt(number);x++){// check if it can divide the numberif(remainder%x===0){// add it as a key to a results objectif(!subdividers[x])subdividers[x]=0;// while it can be a divisor, add +1 to the key and update numberwhile(remainder%x===0){subdividers[x]++;remainder=remainder/x;}}}// if after all there's still a remaining number, it is a divisor tooif(remainder>1){if(!subdividers[remainder])subdividers[remainder]=1;elsesubdividers[remainder]+=1;}returnsubdividers;}// initial dividers: none!letdividers={}// calculate the dividers for each number used in the factorialfor(letx=2;x<=number;x++)dividers=subdecomp(x,dividers);// generate a html string with the resultreturnObject.keys(dividers).reduce((acc,curr)=>dividers[curr]===1?`${acc}${curr}`:`${acc}${curr}<sup>${dividers[curr]}</sup>`,`decomp(${number}) = `);}
Not perfect, but it seems to work now. The previous answer I deleted only calculated for the number itself, and not the factorial.
JavaScript
Not perfect, but it seems to work now. The previous answer I deleted only calculated for the number itself, and not the factorial.
And here is a demo on CodePen.