What is the fastest LAMBDA formula in Excel for prime factorization?
This is just out of personal curiosity, but I'd like to know the fastest prime factorization function in Excel.
Here are the rules:
Only Excel is allowed; VBA is not permitted.
The name manager is available, so it is acceptable to use this function to perform multiple functions.
The input should be an integer n between 2 and 10^15. Since it can handle up to 2^53 internally, a version that supports up to that might be good.
Any good ideas?
My fastest is here (up to 10^15):
=LAMBDA(n,LET(NMOD,LAMBDA(a,b,a-QUOTIENT(a,b)*b),V,REDUCE(VSTACK(n,1),VSTACK(2,3,5,7),LAMBDA(p,q,LET(a,INDEX(p,1,1),b,SUM(IF(NMOD(a,q^SEQUENCE(LOG(a,q)))=0,1,0)),IF(IFERROR(b,0)=0,p,VSTACK(a/q^b,DROP(p,1),SEQUENCE(b,1,q,0)))))),nb,INDEX(V,1,1),seq,SEQUENCE(ROUNDUP(SQRT(nb)/210,0),,0,210)+HSTACK(1,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,121,127,131,137,139,143,149,151,157,163,167,169,173,179,181,187,191,193,197,199,209),M,IFERROR(DROP(TOCOL(IF(NMOD(nb,seq)=0,seq,NA()),2),1),nb),SqrM,FILTER(M,M<=SQRT(MAX(M))),S,IF(ISERROR(INDEX(SqrM,1,1)),M,REDUCE(M,SqrM,LAMBDA(p,q,FILTER(p,(NMOD(p,q)<>0)+(p=q))))),k,nb/PRODUCT(S),L,DROP(VSTACK(DROP(V,1),S),1),add,IF(k=1,1,REDUCE(VSTACK(k,1),S,LAMBDA(p,q,LET(a,INDEX(p,1,1),b,SUM((NMOD(a,q^SEQUENCE(LOG(a,q)))=0)*1),IF(IFERROR(b,0)=0,p,VSTACK(a/q^b,DROP(p,1),SEQUENCE(b,1,q,0))))))),Ans,SORT(VSTACK(L,add)),FILTER(Ans,Ans>1,1)))
[link] [comments]
Want to read more?
Check out the full article on the original site