Using the extremely elegant solution described here.

frommathimportlog,factorialdefsundaram3(max_n):""" Returns a list of all primes under max_n """numbers=list(range(3,max_n+1,2))half=(max_n)//2initial=4forstepinrange(3,max_n+1,2):foriinrange(initial,half,step):numbers[i-1]=0initial+=2*(step+1)ifinitial>half:return[2]+list([_ffor_finnumbersif_f])defdecomp(n):""" Performs factorial prime factorization of n.
Returns a factor: exponent dictionary.
"""return{p:sum(n//p**kforkinrange(1,int(log(n,p))+1))forpinsundaram3(n+1)}defmain():testcases=[5,12,14,17,22,25]forcaseintestcases:factors_str=' * '.join(f'{b}^{p}'forb,pindecomp(case).items())print(f"{case}! = {factors_str}")if__name__=='__main__':main()

We're a place where coders share, stay up-to-date and grow their careers.

Using the extremely elegant solution described here.