Anonymous user
Long multiplication: Difference between revisions
added Ceylon
m (→{{header|Sidef}}: added output) |
(added Ceylon) |
||
Line 1,609:
340282366920938463463374607431768211456
</pre>
=={{Header|Ceylon}}==
<lang Ceylon>"run() is the main function of this module."
shared void run() {
function multiply(String|Integer|Integer[] top, String|Integer|Integer[] bottom, Integer base = 10) {
function fromString(String s) =>
s
.filter(not(','.equals))
.map((char) => Integer.parse(char.string))
.narrow<Integer>()
.sequence()
.reversed;
function toString(Integer[] ints) =>
""
.join(ints.interpose(',', 3).sequence().reversed)
.removeInitial("0")
.removeInitial(",");
function fromInteger(Integer int) => fromString(int.string);
function convertArg(String|Integer|Integer[] arg) =>
switch(arg)
case (is String) fromString(arg)
case (is Integer) fromInteger(arg)
case (is Integer[]) arg;
value a = convertArg(top);
value b = convertArg(bottom);
value p = a.size;
value q = b.size;
value product = Array.ofSize(p + q, 0);
for (bIndex->bDigit in b.indexed) {
variable value carry = 0;
for (aIndex->aDigit in a.indexed) {
assert (exists prodDigit = product[aIndex + bIndex]);
value temp = prodDigit + carry + aDigit * bDigit;
carry = temp / base;
product[aIndex + bIndex] = temp % base;
}
assert (exists lastDigit = product[bIndex + p]);
product[bIndex + p] = lastDigit + carry;
}
return toString(product.sequence());
}
value twoToThe64th = "18,446,744,073,709,551,616";
value expectedResult = "340,282,366,920,938,463,463,374,607,431,768,211,456";
value result = multiply(twoToThe64th, twoToThe64th);
print("The expected result is ``expectedResult`` and the real result was ``result``");
print("Do the match? ``expectedResult == result then "Yes!" else "No!"``");
}</lang>
=={{Header|COBOL}}==
|