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.
Here is a FORTRAN 90 answer (the module and an example program):
MODULEdecomposeIMPLICITNONECONTAINSSUBROUTINEdecomp(n,primes,powers,factorial)INTEGER,INTENT(IN)::nINTEGER,ALLOCATABLE,INTENT(OUT)::primes(:),powers(:)INTEGER,INTENT(OUT)::factorialINTEGER::i,j,m,currentINTEGER,DIMENSION(25)::lowPrimesDATAlowPrimes/2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97/IF(n.GT.100)THEN!can't handle a too big numberPRINT*,"Choose a number less than 100"RETURNENDIFm=COUNT(lowPrimes.LE.n)!how many integers less or equal to n!initial valuesALLOCATE(primes(m),powers(m))primes=lowPrimes(1:m)powers=0factorial=1!decomposing n! number by numberDOi=2,nfactorial=factorial*icurrent=iDOj=1,mIF(MOD(current,primes(j)).EQ.0)THENcurrent=INT(current/primes(j))powers(j)=powers(j)+1ENDIFIF(current.EQ.1)EXIT! we are done decomposing iENDDO!jENDDO!iENDSUBROUTINEdecompENDMODULEdecomposePROGRAMtestDecomposeUSEdecomposeIMPLICITNONEINTEGER::myInt,m,iINTEGER,ALLOCATABLE::pr(:),po(:)INTEGER::famyInt=12CALLdecomp(n=myInt,primes=pr,powers=po,factorial=fa)!printing the result:WRITE(*,"(i3,a4,i12,a3)",advance='no')myInt,"! = ",fa," = "m=SIZE(pr)DOi=1,mIF(po(i).GT.0)WRITE(*,"(i2,a1,i2)",advance='no')pr(i),"^",po(i)IF(i.LT.m)WRITE(*,"(a3)",advance='no')" + "ENDDOPRINT*," "ENDPROGRAMtestDecompose
Here is my Rust version! These are getting long including the tests, so I might have to find a better way to post em here! Maybe I'll start posting embeds to their Github repo 🤔 (But I don't know if I can embed a single file from a repo...)
Anyways here is today's!
I took a optimization, by never actually calculating the full factorial. Instead I do the prime decomposition, on each of the factorial values (2..n) and append those lists of prime numbers together. This list will be the same as a prime decomposition of the full factorial.
From here I count the occurrences of each of the prime numbers and the format a string matching the examples!
import math
var = int(input("Enter the number: "))
fac = math.factorial(var)
def prime(x):
for i in range(3,x-1,2):
if x%i == 0:
return False
return True
def decomp(x,fac):
for i in range(fac):
if fac % x**i != 0:
return i-1
print('2^' + str(decomp(2,fac)), end='')
for i in range(3,var+1,2):
if prime(i) == True:
e = decomp(i,fac)
if e != 1:
print(' * ' + str(i) + '^' + str(e), end='')
else:
print(' * ' + str(i), end='')
else:
continue
print('\n')
Full stack web dev.
Studying FP web development approaches, while helping Mission Bit create paths to programming for underserved public school kids.
Previously @ Gradescope.
I’m not really happy with my solution yet, I feel it could be simpler. I’d rather keep functions that do one thing, though, and avoid decompose_factorial_into_factors_and_group(X, Y, Z, []) or something. Hence I didn’t save on the single-argument versions, or combine unrelated functions.
The decomp function creates a (factor, power) map describing the decomposition of an integer. The decompFactorial function creates this decomposition for all integers from 2 to n, then merges the maps summing the values for each key. The result is then pretty printed.
Passionate about solving problems since 2012! I'm a full stack web developer with experience in AWS, TypeScript and React. Looking for new opportunities!
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.
Woo 3 days in a row!
I'm not too good when it comes to maths and I'm sure there are loads of more efficient algorithms for finding prime factors, but I think this works!
Output:
Turns out there's actually a built in method for this ... So just the formatting I guess...
C++
Output:
Perl solution, not using any math libraries:
For 22, it prints 2¹⁹ * 3⁹ * 5⁴ * 7³ * 11² * 13 * 17 * 19.
Here is a FORTRAN 90 answer (the module and an example program):
The result looks like:
12! = 479001600 = 2^ 6 + 3^ 4 + 5^ 2 + 7^ 1 + 11^ 1
Note: I hard coded n=12 but I could made n be read from the command line
I can use any language right? How about MATLAB ;)
But here's a haskell solution just for fun:
Here is my Rust version! These are getting long including the tests, so I might have to find a better way to post em here! Maybe I'll start posting embeds to their Github repo 🤔 (But I don't know if I can embed a single file from a repo...)
Anyways here is today's!
I took a optimization, by never actually calculating the full factorial. Instead I do the prime decomposition, on each of the factorial values (2..n) and append those lists of prime numbers together. This list will be the same as a prime decomposition of the full factorial.
From here I count the occurrences of each of the prime numbers and the format a string matching the examples!
Here is my simple solution to finish this problem with PHP:
My solution in Swift, factorial numbers are too big for
Intbut it's really bored to deal withNSDecimalNumber:I hardcoded prime numbers but here it is
Python
Clojure:
I’m learning Erlang.
I’m not really happy with my solution yet, I feel it could be simpler. I’d rather keep functions that do one thing, though, and avoid
decompose_factorial_into_factors_and_group(X, Y, Z, [])or something. Hence I didn’t save on the single-argument versions, or combine unrelated functions.Try with:
Too bad @stevemoon didn’t do this one, I would have liked to compare. :-)
Solution
Test
Result
Haskell
The
decompfunction creates a(factor, power)map describing the decomposition of an integer. ThedecompFactorialfunction creates this decomposition for all integers from2ton, then merges the maps summing the values for each key. The result is then pretty printed.None of these examples work with Google's public key...
taking too long.
Any way to speed up?
\s
How are you developing it? Maybe we can take a look?