Long multiplication: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: formatting to improve clarity)
(→‎{{header|J}}: simplify and reorganize)
Line 642: Line 642:


=={{header|J}}==
=={{header|J}}==
'''Solution:'''
<lang j> digits =: ,.&.":
polymult =: +//.@(*/)
buildDecimal=: (+ 10x&*)/@|.


<lang j> ((+ 10x&*)/@|.@(,.&.":@[ +//.@(*/) ,.&.":@]))/ ,~2x^64
buildDecimal@polymult&digits/ ,~ 2x^64
340282366920938463463374607431768211456</lang>
340282366920938463463374607431768211456</lang>

* digits: <code>,.&.": y</code>
'''Alternatives:'''<br>
The above definition may be written as a single verb without the components:
<lang j> (+ 10x&*)/@|.@(+//.@(*/)&(,.&.":))/ ,~2x^64
340282366920938463463374607431768211456</lang>
Or using the primitive dyad <code>#.</code> instead of <code>(+ 10x&*)/@|.</code>:
<lang j> (10x&#.)@(+//.@(*/)&(,.&.":))/ ,~2x^64
340282366920938463463374607431768211456</lang>
Writing directly:
<lang j> (2x^64)*(2x^64)
340282366920938463463374607431768211456</lang>

'''Explaining the component verbs:'''
* <code>digits</code>
<lang j> ,.&.": 123
<lang j> ,.&.": 123
1 2 3</lang>
1 2 3</lang>
* polynomial multiplication: <code>x (+//.@(*/)) y</code> '''ref.''' [http://www.jsoftware.com/help/dictionary/samp23.htm]
* <code>polymult</code> (polynomial multiplication): '''ref.''' [http://www.jsoftware.com/help/dictionary/samp23.htm]
<lang j> 1 2 3 (+//.@(*/)) 1 2 3
<lang j> 1 2 3 (+//.@(*/)) 1 2 3
1 4 10 12 9</lang>
1 4 10 12 9</lang>
* building the decimal result: <code>(+ 10x&*)/|. y</code>
* <code>buildDecimal</code> (building the decimal result):
<lang j> (+ 10x&*)/|. 1 4 10 12 9
<lang j> (+ 10x&*)/|. 1 4 10 12 9
15129</lang>
15129</lang>
or using the primitive dyad <code>#.</code> instead of <code>(+ 10x&*)/@|.</code>
<lang j> (10x #. ,.&.":@[ +//.@(*/) ,.&.":@])/ ,~2x^64
340282366920938463463374607431768211456</lang>
Writing directly:
<lang j> (2x^64)*(2x^64)
340282366920938463463374607431768211456</lang>


=={{header|JavaScript}}==
=={{header|JavaScript}}==