Jump to content

Long multiplication: Difference between revisions

→‎{{header|J}}: simplify and reorganize
(→‎{{header|J}}: formatting to improve clarity)
(→‎{{header|J}}: simplify and reorganize)
Line 642:
 
=={{header|J}}==
'''Solution:'''
<lang j> digits =: ,.&.":
polymult =: +//.@(*/)
buildDecimal=: (+ 10x&*)/@|.
 
<lang j> ((+ 10x&*)/buildDecimal@|.@(,.polymult&.":@[ +digits//.@(*/) ,.&.":@]))/~ ,~2x^64
340282366920938463463374607431768211456</lang>
 
* digits: <code>,.&.": y</code>
'''Alternatives:'''<br>
The above definition may be written as a single verb without the components:
<lang j> (10x #.+ ,.10x&*)/@|.":@[ (+//.@(*/) &(,.&.":@]))/ ,~2x^64
340282366920938463463374607431768211456</lang>
orOr 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
1 2 3</lang>
* polynomial multiplication: <code>x (+//.@(*/)) ypolymult</code> (polynomial multiplication): '''ref.''' [http://www.jsoftware.com/help/dictionary/samp23.htm]
<lang j> 1 2 3 (+//.@(*/)) 1 2 3
1 4 10 12 9</lang>
* <code>buildDecimal</code> (building the decimal result): <code>(+ 10x&*)/|. y</code>
<lang j> (+ 10x&*)/|. 1 4 10 12 9
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}}==
892

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.