Closed JohnBrinkman closed 7 months ago
Some limited support seems possible. Here's a sample that registers a function to add two numbers (represented as strings) by:
split()
reduce()
join()
register("_bigAdd", &( split(@[0], "").reverse(@) + split(@[1], "").reverse(@) ).reduce(@, &{ result: accumulated.result, d: accumulated.carry + current }.{ result: result ~ if(d>=10, d-10, d), carry: if (d >= 10, 1, 0) }, {result: `[]`, carry: 0} ).if(carry, result ~ 1, result) .reverse(@) .join(@, "") )
_bigAdd(["12345", "6789"]) => "19134"
_bigAdd(["12345", "6789"])
"19134"
_bigAdd(["2147483647", "97852516353"]) => "100000000000"
_bigAdd(["2147483647", "97852516353"])
"100000000000"
A brute force multiplication function could be registered by leveraging bigAdd():
bigAdd()
register("_bigMultiply", &{ a1: rept(@[0] & ",", length(@[1])).split(@, ","), a2: @[1].split(@, "").reverse(@) }.zip(a1,a2) .map(@, &rept(@[0] & ",", @[1]).split(@, ",")[?@]) .map(@, &reduce(@, &_bigAdd([accumulated, current]), "0")) .reduce(@, &_bigAdd([accumulated, current & rept("0", index)]), "0"))
Then _bigMultiply(["12345", "6789"]) => "83810205"
_bigMultiply(["12345", "6789"])
"83810205"
Received for information.
Pros:
Cons
Could we implement Big Number math using existing formulas?
Some limited support seems possible. Here's a sample that registers a function to add two numbers (represented as strings) by:
split()
reduce()
join()
:_bigAdd(["12345", "6789"])
=>"19134"
_bigAdd(["2147483647", "97852516353"])
=>"100000000000"
A brute force multiplication function could be registered by leveraging
bigAdd()
:Then
_bigMultiply(["12345", "6789"])
=>"83810205"