Long multiplication: Difference between revisions

Content added Content deleted
(Add Lobster solution)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 972:
}</lang>
 
=={{Headerheader|AWK}}==
{{works with|gawk|3.1.7}}
{{works with|nawk|20100523}}
Line 1,068:
2^64 * 2^64 = 340282366920938463463374607431768211456</pre>
 
=={{Headerheader|BASIC}}==
{{works with|QBasic|}}
 
Line 1,330:
PRINT "THE SOLUTION IN THE FILE: R.MLT"</lang>
 
==={{Headerheader|Applesoft BASIC}}===
<lang ApplesoftBasic> 100 A$ = "18446744073709551616"
110 B$ = A$
Line 1,428:
<pre>340282366920938463463374607431768211456</pre>
 
=={{Headerheader|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Library method:
Line 1,467:
= $$^num3&(0)</lang>
 
=={{Headerheader|C}}==
Doing it as if by hand.
<lang c>#include <stdio.h>
Line 1,524:
return 0;
}</lang>output<lang>340282366920938463463374607431768211456</lang>
 
=={{header|C sharp|C#}}==
{{incorrect|F#|The problem is to implement long multiplication, not to demonstrate bignum support.}}
 
<code>System.Numerics.BigInteger</code> was added with C# 4.
{{works with|C sharp|C#|4+}}
<lang csharp>using System;
using System.Numerics;
 
class Program {
static void Main() {
BigInteger pow2_64 = BigInteger.Pow(2, 64);
BigInteger result = BigInteger.Multiply(pow2_64, pow2_64);
Console.WriteLine(result);
}
}</lang>
 
Output:
<pre>
340282366920938463463374607431768211456
</pre>
 
=={{header|C++}}==
Line 1,698 ⟶ 1,719:
</pre>
 
=={{header|C sharp|C#Ceylon}}==
{{incorrect|F#|The problem is to implement long multiplication, not to demonstrate bignum support.}}
 
<code>System.Numerics.BigInteger</code> was added with C# 4.
{{works with|C sharp|C#|4+}}
<lang csharp>using System;
using System.Numerics;
 
class Program {
static void Main() {
BigInteger pow2_64 = BigInteger.Pow(2, 64);
BigInteger result = BigInteger.Multiply(pow2_64, pow2_64);
Console.WriteLine(result);
}
}</lang>
 
Output:
<pre>
340282366920938463463374607431768211456
</pre>
 
 
=={{Header|Ceylon}}==
<lang Ceylon>"run() is the main function of this module."
 
Line 1,780 ⟶ 1,779:
}</lang>
 
=={{Headerheader|COBOL}}==
<lang COBOL>
identification division.
Line 1,945 ⟶ 1,944:
console.log BcdInteger.to_string square # 340282366920938463463374607431768211456
</lang>
 
 
=={{header|Common Lisp}}==
Line 2,270 ⟶ 2,268:
 
end program Test</lang>
 
=={{header|FreeBASIC}}==
<lang freebasic>' version 08-01-2017
Line 3,397 ⟶ 3,396:
340282366920938463463374607431768211456
</pre>
 
 
=={{header|Maple}}==
Line 3,667 ⟶ 3,665:
[1] (List) [768211456, 374607431, 938463463, 282366920, 340]
ok
</pre>
 
=={{header|Ol}}==
Ol already supports long numbers "out-of-the-box".
 
<lang scheme>
(define x (* 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2)) ; 2^64
 
(print (* x x))
</lang>
<pre>
340282366920938463463374607431768211456
</pre>
 
Line 3,896 ⟶ 3,906:
my $onetwentyeight = &longhand_multiplication($sixtyfour, $sixtyfour);
print "$onetwentyeight\n";</lang>
 
=={{header|Perl 6}}==
{{works with|rakudo|2015-09-17}}
For efficiency (and novelty), this program explicitly implements long multiplication, but in base 10000. That base was chosen because multiplying two 5-digit numbers can overflow a 32-bit integer, but two 4-digit numbers cannot.
<lang perl6>sub num_to_groups ( $num ) { $num.flip.comb(/.**1..4/)».flip };
sub groups_to_num ( @g ) { [~] flat @g.pop, @g.reverse».fmt('%04d') };
 
sub long_multiply ( Str $x, Str $y ) {
my @group_sums;
for flat num_to_groups($x).pairs X num_to_groups($y).pairs -> $xp, $yp {
@group_sums[ $xp.key + $yp.key ] += $xp.value * $yp.value;
}
 
for @group_sums.keys -> $k {
next if @group_sums[$k] < 10000;
@group_sums[$k+1] += @group_sums[$k].Int div 10000;
@group_sums[$k] %= 10000;
}
 
return groups_to_num @group_sums;
}
 
my $str = '18446744073709551616';
long_multiply( $str, $str ).say;
 
# cross-check with native implementation
say +$str * +$str;</lang>
 
{{out}}
<pre>
340282366920938463463374607431768211456
340282366920938463463374607431768211456
</pre>
 
=={{header|Phix}}==
Line 4,535 ⟶ 4,512:
longmult(2 ** 64, 2 ** 64)
)</lang>
 
=={{header|Ol}}==
Ol already supports long numbers "out-of-the-box".
 
<lang scheme>
(define x (* 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2)) ; 2^64
 
(print (* x x))
</lang>
<pre>
340282366920938463463374607431768211456
</pre>
 
=={{header|R}}==
Line 4,666 ⟶ 4,631:
;; 340282366920938463463374607431768211456
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-17}}
For efficiency (and novelty), this program explicitly implements long multiplication, but in base 10000. That base was chosen because multiplying two 5-digit numbers can overflow a 32-bit integer, but two 4-digit numbers cannot.
<lang perl6>sub num_to_groups ( $num ) { $num.flip.comb(/.**1..4/)».flip };
sub groups_to_num ( @g ) { [~] flat @g.pop, @g.reverse».fmt('%04d') };
 
sub long_multiply ( Str $x, Str $y ) {
my @group_sums;
for flat num_to_groups($x).pairs X num_to_groups($y).pairs -> $xp, $yp {
@group_sums[ $xp.key + $yp.key ] += $xp.value * $yp.value;
}
 
for @group_sums.keys -> $k {
next if @group_sums[$k] < 10000;
@group_sums[$k+1] += @group_sums[$k].Int div 10000;
@group_sums[$k] %= 10000;
}
 
return groups_to_num @group_sums;
}
 
my $str = '18446744073709551616';
long_multiply( $str, $str ).say;
 
# cross-check with native implementation
say +$str * +$str;</lang>
 
{{out}}
<pre>
340282366920938463463374607431768211456
340282366920938463463374607431768211456
</pre>
 
=={{header|REXX}}==
Line 5,189 ⟶ 5,188:
0340282366920938463463374607431768211456
</pre>
 
 
=={{header|XPL0}}==