Long multiplication: Difference between revisions

m
whitespace, minor corrections
m (→‎{{header|Scheme}}: whitespace)
m (whitespace, minor corrections)
Line 34:
return (0 => 0);
end "*";</lang>
 
The task requires conversion into decimal base. For this we also need division to short number with a remainder. Here it is:
<lang ada>procedure Div
Line 56 ⟶ 57:
Last := Size;
end Div;</lang>
 
With the above the test program:
<lang ada>with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 83 ⟶ 85:
Put (X);
end Long_Multiplication;</lang>
 
Sample output:
<pre>
340282366920938463463374607431768211456
</pre>
 
=={{header|ALGOL 68}}==
The long multiplication for the golden ratio has been included as half the digits
cancel and end up as being zero. This is useful for testing.
 
===Built in or standard distribution routines===
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
Line 220 ⟶ 225:
IF leading zeros = UPB out THEN "0" ELSE sign + out[leading zeros+1:] FI
);</lang>
 
<lang algol68>################################################################
# Finally Define the required INTEGER multiplication OPerator. #
Line 243 ⟶ 249:
NORMALISE ab
);</lang>
 
<lang algol68># The following standard operators could (potentially) also be defined #
OP - = (INTEGER a)INTEGER: raise integer not implemented error("monadic minus"),
Line 273 ⟶ 280:
print(("2 ** 64 * -(2 ** 64) = ", REPR (two to the power of 64 * neg two to the power of 64), new line))
)</lang>
 
Output:
<pre>
Line 280 ⟶ 288:
2 ** 64 * -(2 ** 64) = -340,282,366,920,938,463,463,374,607,431,768,211,456
</pre>
 
===Other libraries or implementation specific extensions===
As of February 2009 no open source libraries to do this task have been located.
 
=={{header|AutoHotkey}}==
ahk [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=149 discussion]
Line 306 ⟶ 316:
=={{Header|AWK}}==
{{works with|gawk|3.1.0}}
 
{{trans|Tcl}}
<lang awk>BEGIN {
Line 399 ⟶ 408:
<pre>2^64 * 2^64 = 340282366920938463463374607431768211456
2^64 * 2^64 = 340282366920938463463374607431768211456</pre>
 
=={{Header|Basic}}==
{{works with|QBasic|}}
 
===Version 1:===
 
<lang qbasic>'PROGRAM : BIG MULTIPLICATION VER #1
'LRCVS 01.01.2010
Line 552 ⟶ 561:
END SUB</lang>
 
===Version 2===
Version 2: <!-- I'm not sure what the difference is; don't feel like reading through them, I was just making sure they work and bughunting. -->
 
<lang qbasic>'PROGRAM: BIG MULTIPLICATION VER # 2
'LRCVS 01/01/2010
Line 663 ⟶ 672:
 
=={{Header|C}}==
 
<lang c>#include <stdio.h>
#include <stdlib.h>
Line 731 ⟶ 739:
 
===Using GMP (GNU Multi Precision library)===
 
{{libheader|GMP}}
<lang c>#include <stdio.h>
Line 751 ⟶ 758:
=={{header|C++}}==
using the Class Library for Numbers , CLN, and g++
<lang cpp>#include <cln/integer.h>//for mathematical operations on arbitrarily long integers
#include <cln/integer_io.h>//for input/output of long integers
#include <iostream>
Line 762 ⟶ 769:
return 0 ;
}</lang>
 
<pre>Output: The result of 2^64 * 2^64 is 340282366920938463463374607431768211456 !
<pre>
<pre>Output: The result of 2^64 * 2^64 is 340282366920938463463374607431768211456 !
</pre>
 
=={{header|C sharp|C#}}==
<code>System.Numerics.BigInteger</code> was added with C# 4.
 
{{works with|C sharp|C#|4+}}
 
<lang csharp>using System;
using System.Numerics;
Line 787 ⟶ 794:
 
=={{header|Common Lisp}}==
 
<lang lisp>(defun number->digits (number)
(do ((digits '())) ((zerop number) digits)
Line 970 ⟶ 976:
 
=={{header|Haskell}}==
 
<lang haskell>digits :: Integer -> [Integer]
digits = map (fromIntegral.digitToInt) . show
Line 985 ⟶ 990:
340282366920938463463374607431768211456</lang>
 
== {{header|Icon and Unicon }}==
Icon and Unicon supportsupports large integers.
==={{headerworks with|Unicon}}===
==={{header|Icon}}===
<lang Icon>procedure main()
write(2^64*2^64)
end</lang>
==={{header|Unicon}}===
This Icon solution works in Unicon.
 
=={{header|J}}==
Line 1,030 ⟶ 1,033:
This is a straight-forward implementation of Long multiplication.
It works with numbers of any length since it uses BigInteger.
<lang java>import java.math.BigInteger;
import java.math.BigInteger;
 
public class LongMult {
Line 1,043 ⟶ 1,045:
return a.multiply(b);
}
}</lang>
}
</lang>
Output:
<langpre>
340282366920938463463374607431768211456
</langpre>
 
=={{header|JavaScript}}==
<lang javascript>function mult(num1,num2){
function mult(num1,num2){
var a1 = num1.split("").reverse();
var a2 = num2.split("").reverse();
Line 1,069:
}
return aResult.reverse().join("");
}</lang>
}
 
</lang>
 
=={{header|Liberty BASIC}}==
Line 1,078 ⟶ 1,076:
=={{header|Mathematica}}==
We define the long multiplication function:
<lang Mathematica> LongMultiplication[a_,b_]:=Module[{d1,d2},
LongMultiplication[a_,b_]:=Module[{d1,d2},
d1=IntegerDigits[a]//Reverse;
d2=IntegerDigits[b]//Reverse;
Sum[d1[[i]]d2[[j]]*10^(i+j-2),{i,1,Length[d1]},{j,1,Length[d2]}]
]</lang>
 
</lang>
Example:
<lang Mathematica> n1 = 2^64;
n1 = 2^64;
n2 = 2^64;
LongMultiplication[n1, n2]</lang>
 
</lang>
gives back:
<lang Mathematica> 340282366920938463463374607431768211456</lang>
 
340282366920938463463374607431768211456
</lang>
To check the speed difference between built-in multiplication (which is already arbitrary precision) we multiply two big numbers (2^8000 has '''2409''' digits!) and divide their timings:
<lang Mathematica> n1=2^8000;
n1=2^8000;
n2=2^8000;
Timing[LongMultiplication[n1,n2]][[1]]
Timing[n1 n2][[1]]
Floor[%%/%]</lang>
 
</lang>
gives back:
<lang Mathematica> 72.9686
72.9686
7.*10^-6
10424088</lang>
 
</lang>
So our custom function takes about 73 second, the built-in function a couple of millionths of a second, so the long multiplication is about 10.5 million times slower! Mathematica uses Karatsuba multiplication for large integers, which is several magnitudes faster for really big numbers. Making it able to multiply <math>3^{(10^7)}\times3^{(10^7)}</math> in about a second; the final result has 9542426 digits; result omitted for obvious reasons.
 
Line 1,191 ⟶ 1,184:
 
In any case, integers are specced to be arbitrarily large, which at least one implementation supports already:
</pre>
<lang>pugs> 18446744073709551616 * 18446744073709551616
340282366920938463463374607431768211456</lang>
340282366920938463463374607431768211456
</langpre>
 
=={{header|PicoLisp}}==
Line 1,199 ⟶ 1,194:
 
=={{header|PL/I}}==
<lang PL/I>/* Multiply a by b, giving c. */
/* Multiply a by b, giving c. */
multiply: procedure (a, b, c);
declare (a, b, c) (*) fixed decimal (1);
Line 1,246 ⟶ 1,240:
a(i) = s;
end;
end complement;</lang>
</lang>
Calling sequence:
<lang PL/I> a = 0; b = 0; c = 0;
<lang>
a = 0; b = 0; c = 0;
a(60) = 1;
do i = 1 to 64; /* Generate 2**64 */
Line 1,260 ⟶ 1,252:
call multiply (a, b, c);
put skip;
call output (c);</lang>
</lang>
Final output:
<langpre>
18446744073709551616
340282366920938463463374607431768211456
</langpre>
 
=={{header|PureBasic}}==
Line 1,286 ⟶ 1,277:
(Note that Python comes with arbitrary length integers).
 
<lang python>#!/usr/bin/env python
print 2**64*2**64</lang>
#!/usr/bin/env python
print 2**64*2**64
</lang>
 
 
{{works with|Python|3.0}}
 
{{trans|Perl}}
<lang python>#!/usr/bin/env python
Line 1,355 ⟶ 1,342:
===Using GMP===
{{libheader|gmp}}
<lang R>library(gmp)
library(gmp)
a <- as.bigz("18446744073709551616")
mul.bigz(a,a)</lang>
</lang>
"340282366920938463463374607431768211456"
===A native implementation===
This code is more verbose than necessary, for ease of understanding.
<lang R>longmult <- function(xstr, ystr)
<lang R>
longmult <- function(xstr, ystr)
{
#get the number described in each string
Line 1,429 ⟶ 1,413:
 
a <- "18446744073709551616"
longmult(a, a)</lang>
</langpre>
"340282366920938463463374607431768211456"
</langpre>
 
=={{header|REXX}}==
<lang REXX>/* long multiply */
/* long multiply */
numeric digits 100
say 2**64*2**64</lang>
</lang>
 
=={{header|Ruby}}==
Line 1,560 ⟶ 1,544:
 
=={{header|Smalltalk}}==
<lang smalltalk>(2 raisedTo: 64) * (2 raisedTo: 64).</lang>
<pre>
(2 raisedTo: 64) * (2 raisedTo: 64).
</pre>
 
=={{header|Tcl}}==
Anonymous user