Jump to content

Long multiplication: Difference between revisions

Added Quackery.
m (→‎{{header|Icon}} and {{header|Unicon}}: Regularize non-standard header markup)
(Added Quackery.)
Line 4,758:
longmult(2 ** 64, 2 ** 64)
)</lang>
 
 
=={{header|Quackery}}==
 
Long multiplication as it was taught at primary school, using natural numbers (including zero) in the base 10 numeral system, with a slight variation in that it maintains a running total rather than adding up the intermediate results column-wise at the end. Starting point is "learn your tables". Presumptions are an understanding of "equal to" and "not equal to", and the ability to split a one or two digit number into tens and units.
 
(This is achieved using <code>/mod</code> to keep the tables compact. Numbers in the addition and multiplication tables could be represented as, for example, <code>[ 8 1 ]</code> instead of <code>81</code> and <code>10 /mod</code> replaced with <code>unpack</code> or, as the nest only contains numbers, <code>do</code>.)
 
In addition to the specified task, we were always encouraged to show our workings.
 
<lang Quackery> [ [] swap witheach
[ char 0 -
swap join ] ] is $->long ( $ --> L )
 
[ number$ $->long ] is long ( n --> L )
[ reverse behead
swap witheach
[ swap 10 * + ] ] is long->num ( L --> n )
 
[ reverse
witheach echo ] is echolong ( L --> )
 
[ over size
over size -
dup dip
[ 0 < if swap ]
abs times
[ 0 join ] ] is zeropad ( L L --> L L )
 
[ [ table
[ 0 1 2 3 4 5 6 7 8 9 ]
[ 1 2 3 4 5 6 7 8 9 10 ]
[ 2 3 4 5 6 7 8 9 10 11 ]
[ 3 4 5 6 7 8 9 10 11 12 ]
[ 4 5 6 7 8 9 10 11 12 13 ]
[ 5 6 7 8 9 10 11 12 13 14 ]
[ 6 7 8 9 10 11 12 13 14 15 ]
[ 7 8 9 10 11 12 13 14 15 16 ]
[ 8 9 10 11 12 13 14 15 16 17 ]
[ 9 10 11 12 13 14 15 16 17 18 ] ]
swap peek 10 /mod ] is add ( n n --> n n )
 
[ dip add add dip [ add nip ] swap ] is addc ( n n c --> n c )
 
[ zeropad
0 temp put
[] unrot witheach
[ dip behead
temp take
addc
temp put
rot swap join swap ]
drop
temp take dup 0 !=
iff join else drop ] is longadd ( L L --> L )
 
[ [ table
[ 0 0 0 0 0 0 0 0 0 0 ]
[ 0 1 2 3 4 5 6 7 8 9 ]
[ 0 2 4 6 8 10 12 14 16 18 ]
[ 0 3 6 9 12 15 18 21 24 27 ]
[ 0 4 8 12 16 20 24 28 32 36 ]
[ 0 5 10 15 20 25 30 35 40 45 ]
[ 0 6 12 18 24 30 36 42 48 54 ]
[ 0 7 14 21 28 35 42 49 56 63 ]
[ 0 8 16 24 32 40 48 56 64 72 ]
[ 0 9 18 27 36 45 54 63 72 81 ] ]
swap peek 10 /mod ] is mult ( n n --> n n )
 
[ dip mult add dip [ add nip ] swap ] is multc ( n n c --> n c )
 
[ dup 0 = iff
[ 2drop 0 long ] done
0 temp put
[] unrot swap witheach
[ over temp take
multc
temp put
swap dip join ]
drop
temp take dup 0 !=
iff join else drop ] is shortmult ( L n --> L )
 
[ ' [ 0 ] over != iff
[ 0 swap join ] ] is timesten ( L --> L )
 
[ dup 0 long = iff
[ 2drop 0 long ] done
0 long unrot
witheach
[ dip dup shortmult
rot longadd swap
timesten ]
drop ] is longmult ( L L --> L )
 
 
( ------------------------ additional to task ------------------------ )
 
[ stack ] is linelength ( --> s )
 
[ linelength share times
[ char - emit ]
cr ] is separator ( --> )
 
[ linelength share
over size - times sp
echolong cr ] is showlong ( --> )
 
[ over size
over size + linelength put
over showlong
dup showlong
separator
dup 0 long = iff
[ 2drop 0 long ] done
0 long unrot
witheach
[ dip dup shortmult
dup showlong
rot longadd swap
timesten ]
separator
swap showlong
separator
drop linelength release ] is workings ( L L --> )
 
 
say "Using long multiplication: "
2 64 ** long dup longmult dup echolong cr
 
say "Using built-in arithmetic: "
2 128 ** dup echo cr cr
 
swap long->num = iff
say "10/10, Gold star!"
else
say "0/10, See me after class."
 
cr cr
say "(Show your workings.)" cr cr
2 64 ** long dup workings cr</lang>
 
{{out}}
 
<pre>Using long multiplication: 340282366920938463463374607431768211456
Using built-in arithmetic: 340282366920938463463374607431768211456
 
10/10, Gold star!
 
(Show your workings.)
 
18446744073709551616
18446744073709551616
----------------------------------------
110680464442257309696
184467440737095516160
11068046444225730969600
18446744073709551616000
922337203685477580800000
9223372036854775808000000
166020696663385964544000000
0
12912720851596686131200000000
55340232221128654848000000000
1291272085159668613120000000000
0
73786976294838206464000000000000
737869762948382064640000000000000
12912720851596686131200000000000000
110680464442257309696000000000000000
737869762948382064640000000000000000
7378697629483820646400000000000000000
147573952589676412928000000000000000000
184467440737095516160000000000000000000
----------------------------------------
340282366920938463463374607431768211456
----------------------------------------</pre>
 
=={{header|R}}==
1,462

edits

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