Skip to content

Instantly share code, notes, and snippets.

@jbetz34
Last active February 11, 2023 07:35
Show Gist options
  • Save jbetz34/9ef3c420a07aef5a83311d18bf0d4b18 to your computer and use it in GitHub Desktop.
Save jbetz34/9ef3c420a07aef5a83311d18bf0d4b18 to your computer and use it in GitHub Desktop.
Multiplication beyond the 18 digit limit
// ORIGINAL METHOD - EXPONENTS
v:{[x] "J"$/:string x}
mult:{[x;y]
m:reverse sum v[x]*/:{x*prd each #\:[;10]reverse til count x}v y;
while[any 9<m;m:(0^next p)+m-10*p:div[;10]m:0f,m];
(?[;1b]"b"$m)_m
}
pwr:{(mult/)y#x}
// NEW METHOD - MATRICES
vec:{"F"$/:string x};
mult2:{[x;y]
m:vec[y] mmu c (rotate[-1]@)\vec[x],(c:-1+count vec y)#0f;
while[any 9<m;m:(0^next p)+m-10*p:div[;10]m:0f,m];
(?[;1b]"b"$m)_m
}
// LARGE NUMBER MATRICES
rt:{(0N,x)##[(x*ceiling count[y]%x)-count y;"0"],y} / reverse take
vx:{"F"$/:rt[x] raze string y} / vectorize in x sized groups
multx:{[n;x;y]
m:vx[n;y] mmu c (rotate[-1]@)\vx[n;x],(c:-1+count vx[n;y])#0f;
while[any m>sum(n-1)(10*)\9;m:(0^next p)+m-prd[n#10]*p:div[;prd n#10]m:0f,m];
(?[;1b]"b"$m)_m
}
fmtx:{[n;x]raze string[x 0],(neg[n]##[n;"0"],)'[string 1_x]}
wfmt:{({x,",",y}/) @[;0;string "F"$] rt[3] raze string x}
// NO WORDS ALLOWED !!
kr:"k""{,/|(0;(#y)-1)_y}" / simplified k conversion of rotate
krt:"k""{(0N,x)##[(x*-_-(#y)%x)-#y;\"0\"],y}" / direct k conversion of rt
kvx:"k""{\"F\"$/:krt[x;,/$:y]}" / direct k conversion of vx
kfmt:"k""{,/($:y 0),((-x)##[x;\"0\"],)'$:1_y}" / direct k conversion of fmtx
kwfmt:"k""{{x,\",\",y}/@[;0;$:\"F\"$]rt[3;,/$:x]}"
multk:{{(?[;1b]"b"$x)_x}{(|/)y>(+/)(x-1)(10*)\9}{(1_p,0f)+y-((*/)x#10)*p:(_:)%[;((*/)x#10)]y:0f,y}/kvx[x;z] mmu c kr\kvx[x;y],(c:-1+(#:)kvx[x]z)#0f}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment