Task

Explicitly implement   long multiplication.

This is one possible approach to arbitrary-precision integer algebra.



For output, display the result of    264 * 264.

Optionally, verify your result against builtin arbitrary precision support.

The decimal representation of    264    is:

18,446,744,073,709,551,616

The output of    264 * 264    is    2128,    and is:

340,282,366,920,938,463,463,374,607,431,768,211,456



package require Tcl 8.5
 
proc longmult {x y} {
    set digits [lreverse [split $x ""]]
    set result {0}
    set j -2
    foreach m [lreverse [split $y ""]] {
	set c 0
	set i [incr j]
	foreach d $digits {
	    set v [lindex $result [incr i]]
	    if {$v eq ""} {
		lappend result 0
		set v 0
	    }
	    regexp (.)(.)$ 0[expr {$v + $c + $d*$m}] -> c v
	    lset result $i $v
	}
	lappend result $c
    }
    # Reconvert digit list into a decimal number
    set result [string trimleft [join [lreverse $result] ""] 0]
    if {$result == ""} then {return 0} else {return $result}
}
 
puts [set n [expr {2**64}]]
puts [longmult $n $n]
puts [expr {$n * $n}]