(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 12784, 449]*) (*NotebookOutlinePosition[ 13839, 485]*) (* CellTagsIndexPosition[ 13795, 481]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{Cell[TextData["Long Multiplication and Division"], "Title", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["Examples"], "Section", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{Cell[TextData["LongMultiply[123, 456]"], "Input", AspectRatioFixed->True], Cell[TextData[" 123"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 456"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" ____"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 738"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 615 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 492 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["______"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 56088"], "Print", Evaluatable->False, AspectRatioFixed->True]}, Open]], Cell[CellGroupData[{Cell[TextData["LongMultiply[456, 123]"], "Input", AspectRatioFixed->True], Cell[TextData[" 456"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 123"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" ____"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 1368"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 912 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 456 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["______"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 56088"], "Print", Evaluatable->False, AspectRatioFixed->True]}, Open]], Cell[TextData["Wow! The same answer! "], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{Cell[TextData["LongDivide[56088, 123]"], "Input", AspectRatioFixed->True], Cell[TextData[" 456"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" ______"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["123 ) 56088"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 492 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" ____ "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 688 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 615 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" ____ "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 738"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 738"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" ____"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 0"], "Print", Evaluatable->False, AspectRatioFixed->True]}, Open]], Cell[CellGroupData[{Cell[TextData["LongDivide[56088, 456]"], "Input", AspectRatioFixed->True], Cell[TextData[" 123"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" ______"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["456 ) 56088"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 456 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" ____ "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 1048 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 912 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" ____ "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 1368"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 1368"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" ____"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 0"], "Print", Evaluatable->False, AspectRatioFixed->True]}, Open]], Cell[CellGroupData[{Cell[TextData["LongMultiply[123456789, 987654321]"], "Input", AspectRatioFixed->True], Cell[TextData[" 123456789"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 987654321"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" __________"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 123456789"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 246913578 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 370370367 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 493827156 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 617283945 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 740740734 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 864197523 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 987654312 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["1111111101 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["__________________"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["121932631112635269"], "Print", Evaluatable->False, AspectRatioFixed->True]}, Open]], Cell[TextData["Here's a way to look at how numbers are carried. "], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{Cell[TextData["LongMultiply[9, 123456789]"], "Input", AspectRatioFixed->True], Cell[TextData[" 9"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 123456789"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["__________"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 81"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 72 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 63 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 54 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 45 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 36 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 27 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 18 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[" 9 "], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["__________"], "Print", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["1111111101"], "Print", Evaluatable->False, AspectRatioFixed->True]}, Open]], Cell[CellGroupData[{Cell[TextData["Implementation"], "Section", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{Cell[TextData["RightJustify and RightIndent"], "Subsection", Evaluatable->False, InitializationCell->True, AspectRatioFixed->True], Cell[TextData[ "RightJustify::usage = \"RightJustify[x, w] \ngives a string of width w with \ the number x \nin the rightmost places.\";"], "Input", InitializationCell->True, AspectRatioFixed->True], Cell[TextData[ "RightJustify[number_, width_] := \n\tStringTake[\n\t\tStringJoin[\n\t\t\t\ Table[\" \", {width}], \n\t\t\tToString @ number\n\t\t], \n\t-width]"], "Input", InitializationCell->True, AspectRatioFixed->True], Cell[TextData[ "RightIndent::ussage = \"RightIndent[x, {i}] gives the \nthe number x as a \ string with i trailing blanks.\";"], "Input", InitializationCell->True, AspectRatioFixed->True], Cell[TextData[ "RightIndent[number_, {indent_}] := \n\tStringJoin[\n\t\tToString @ number, \n\ \t\tTable[\" \", {indent - 1}]\n\t]"], "Input", InitializationCell->True, AspectRatioFixed->True]}, Open]], Cell[CellGroupData[{Cell[TextData["LongMultiply"], "Subsection", Evaluatable->False, InitializationCell->True, AspectRatioFixed->True], Cell[TextData[ "LongMultiply::usage = \"LongMultiply[x, y] \nshows the long multiplication \ of x by y. \nThe numbers x and y must be non-negative integers.\";"], "Input",\ InitializationCell->True, AspectRatioFixed->True], Cell[TextData[ "LongMultiply[x_, y_] := Module[\n{X, Y, width},\n{X, Y} = Map[IntegerDigits, \ {x, y}];\nwidth = Length[Join @@ {X, Y}];\nPrint @ RightJustify[x, width];\n\ Print @ RightJustify[y, width];\nPrint @ RightJustify[\n\tStringJoin @@ \n\t\t\ Table[\"_\", {1 + Max @ Map[Length, {X, Y}]}],\n\twidth\n];\nMap[\n\tPrint, \n\ \tMap[RightJustify[#, width]&, \n\t\tMapIndexed[\n\t\t\tRightIndent, \n\t\t\t\ Map[\n\t\t\t\tRightJustify[#, width]&, \n\t\t\t\tReverse @ IntegerDigits[y] x \ \n\t\t\t]\n\t\t]\n\t]\n];\nPrint[StringJoin @@ Table[\"_\", {width}]];\nPrint \ @ RightJustify[x y, width];\n]"], "Input", InitializationCell->True, AspectRatioFixed->True]}, Open]], Cell[CellGroupData[{Cell[TextData["LongDivision"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[ "LongDivide::usage = \"LongDivide[x, y] \nshows the long division of x by y. \ \nThe numbers x and y must be non-negative integers.\";"], "Input", InitializationCell->True, AspectRatioFixed->True], Cell[TextData[ "LongDivide[x_, y_] := Module[\n{q, m, X, Y, Q, P, R, width},\nq = \ Quotient[x, y];\nm = Mod[x, y];\n{X, Y, Q} = Map[IntegerDigits, {x, y, q}];\n\ P = Reverse[y Q];\nR = Rest @ Reverse @ Prepend[\n\tFoldList[Floor[#2 + #1 \ /10]&, m + First @ P, Rest @ P],\n\tm\n];\nwidth = 3 + Plus @@ Map[Length, \ {X, Y}];\nPrint @ RightJustify[ToString @ q, width];\nPrint @ StringJoin[\n\t\ Table[\" \", {2 + Length @ Y}], \n\tTable[\"_\", {1 + Length @ X}]];\nPrint @ \ StringJoin[ToString @ y, \" ) \", ToString @ x];\nMap[\n\tLongDivideAux[X, Y, \ Q, width, #]&, \n\t\tTranspose @ {y Q, R, Range @ Length @ R}\n];\n]"], "Input", InitializationCell->True, AspectRatioFixed->True], Cell[TextData[ "LongDivideAux[X_, Y_, Q_, width_, {yq_, r_, n_}] := \nModule[\n{},\n\tPrint \ @ RightJustify[\n\t\tRightIndent[\n\t\t\tRightJustify[\n\t\t\t\tToString @ \ yq, \n\t\t\t\twidth\n\t\t\t], \n\t\t\t{Length @ Q - n + 1}\n\t\t], \n\t\t\ width\n\t];\n\tPrint @ RightJustify[\n\t\tRightIndent[\n\t\t\tRightJustify[\n\ \t\t\t\tStringJoin @ Table[\"_\", {1 + Length @ Y}],\n\t\t\t\twidth], \n\t\t\t\ {Length @ Q - n + 1}\n\t\t],\n\t\twidth\n\t];\t\n\tPrint @ RightJustify[\n\t\t\ RightIndent[\n\t\t\tRightJustify[\n\t\t\t\tToString @ r, \n\t\t\t\twidth\n\t\t\ \t], \n\t\t\t{Length @ Q - n}\n\t\t], \n\t\twidth\n\t];\n]\n\n"], "Input", InitializationCell->True, AspectRatioFixed->True]}, Open]]}, Open]]}, Open]] }, FrontEndVersion->"Macintosh 3.0", ScreenRectangle->{{0, 640}, {0, 460}}, WindowToolbars->{}, CellGrouping->Manual, WindowSize->{520, 365}, WindowMargins->{{20, Automatic}, {Automatic, 16}}, PrivateNotebookOptions->{"ColorPalette"->{RGBColor, -1}}, ShowCellLabel->True, ShowCellTags->False, RenderingOptions->{"ObjectDithering"->True, "RasterDithering"->False}, MacintoshSystemPageSetup->"\<\ AVU/IFiQKFD000000Tn0D09?RA0000000PBJL0901kP0AP1Y06`0I@1^0642Ch1@ 0Tn9<000000219Y@0T07^000000000000000009?P50000000000000000000000 00000000000000000000000000000000\>" ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1731, 51, 107, 2, 70, "Title", Evaluatable->False], Cell[1841, 55, 85, 2, 70, "Section", Evaluatable->False], Cell[CellGroupData[{ Cell[1949, 59, 75, 1, 70, "Input"], Cell[2027, 62, 81, 2, 70, "Print", Evaluatable->False], Cell[2111, 66, 81, 2, 70, "Print", Evaluatable->False], Cell[2195, 70, 81, 2, 70, "Print", Evaluatable->False], Cell[2279, 74, 81, 2, 70, "Print", Evaluatable->False], Cell[2363, 78, 81, 2, 70, "Print", Evaluatable->False], Cell[2447, 82, 81, 2, 70, "Print", Evaluatable->False], Cell[2531, 86, 81, 2, 70, "Print", Evaluatable->False], Cell[2615, 90, 81, 2, 70, "Print", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[2728, 94, 75, 1, 70, "Input"], Cell[2806, 97, 81, 2, 70, "Print", Evaluatable->False], Cell[2890, 101, 81, 2, 70, "Print", Evaluatable->False], Cell[2974, 105, 81, 2, 70, "Print", Evaluatable->False], Cell[3058, 109, 81, 2, 70, "Print", Evaluatable->False], Cell[3142, 113, 81, 2, 70, "Print", Evaluatable->False], Cell[3226, 117, 81, 2, 70, "Print", Evaluatable->False], Cell[3310, 121, 81, 2, 70, "Print", Evaluatable->False], Cell[3394, 125, 81, 2, 70, "Print", Evaluatable->False] }, Open ]], Cell[3487, 129, 96, 2, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[3606, 133, 75, 1, 70, "Input"], Cell[3684, 136, 86, 2, 70, "Print", Evaluatable->False], Cell[3773, 140, 86, 2, 70, "Print", Evaluatable->False], Cell[3862, 144, 86, 2, 70, "Print", Evaluatable->False], Cell[3951, 148, 86, 2, 70, "Print", Evaluatable->False], Cell[4040, 152, 86, 2, 70, "Print", Evaluatable->False], Cell[4129, 156, 86, 2, 70, "Print", Evaluatable->False], Cell[4218, 160, 86, 2, 70, "Print", Evaluatable->False], Cell[4307, 164, 86, 2, 70, "Print", Evaluatable->False], Cell[4396, 168, 86, 2, 70, "Print", Evaluatable->False], Cell[4485, 172, 86, 2, 70, "Print", Evaluatable->False], Cell[4574, 176, 86, 2, 70, "Print", Evaluatable->False], Cell[4663, 180, 86, 2, 70, "Print", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[4781, 184, 75, 1, 70, "Input"], Cell[4859, 187, 86, 2, 70, "Print", Evaluatable->False], Cell[4948, 191, 86, 2, 70, "Print", Evaluatable->False], Cell[5037, 195, 86, 2, 70, "Print", Evaluatable->False], Cell[5126, 199, 86, 2, 70, "Print", Evaluatable->False], Cell[5215, 203, 86, 2, 70, "Print", Evaluatable->False], Cell[5304, 207, 86, 2, 70, "Print", Evaluatable->False], Cell[5393, 211, 86, 2, 70, "Print", Evaluatable->False], Cell[5482, 215, 86, 2, 70, "Print", Evaluatable->False], Cell[5571, 219, 86, 2, 70, "Print", Evaluatable->False], Cell[5660, 223, 86, 2, 70, "Print", Evaluatable->False], Cell[5749, 227, 86, 2, 70, "Print", Evaluatable->False], Cell[5838, 231, 86, 2, 70, "Print", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[5956, 235, 87, 1, 70, "Input"], Cell[6046, 238, 93, 2, 70, "Print", Evaluatable->False], Cell[6142, 242, 93, 2, 70, "Print", Evaluatable->False], Cell[6238, 246, 93, 2, 70, "Print", Evaluatable->False], Cell[6334, 250, 93, 2, 70, "Print", Evaluatable->False], Cell[6430, 254, 93, 2, 70, "Print", Evaluatable->False], Cell[6526, 258, 93, 2, 70, "Print", Evaluatable->False], Cell[6622, 262, 93, 2, 70, "Print", Evaluatable->False], Cell[6718, 266, 93, 2, 70, "Print", Evaluatable->False], Cell[6814, 270, 93, 2, 70, "Print", Evaluatable->False], Cell[6910, 274, 93, 2, 70, "Print", Evaluatable->False], Cell[7006, 278, 93, 2, 70, "Print", Evaluatable->False], Cell[7102, 282, 93, 2, 70, "Print", Evaluatable->False], Cell[7198, 286, 93, 2, 70, "Print", Evaluatable->False], Cell[7294, 290, 93, 2, 70, "Print", Evaluatable->False] }, Open ]], Cell[7399, 294, 123, 2, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[7545, 298, 79, 1, 70, "Input"], Cell[7627, 301, 85, 2, 70, "Print", Evaluatable->False], Cell[7715, 305, 85, 2, 70, "Print", Evaluatable->False], Cell[7803, 309, 85, 2, 70, "Print", Evaluatable->False], Cell[7891, 313, 85, 2, 70, "Print", Evaluatable->False], Cell[7979, 317, 85, 2, 70, "Print", Evaluatable->False], Cell[8067, 321, 85, 2, 70, "Print", Evaluatable->False], Cell[8155, 325, 85, 2, 70, "Print", Evaluatable->False], Cell[8243, 329, 85, 2, 70, "Print", Evaluatable->False], Cell[8331, 333, 85, 2, 70, "Print", Evaluatable->False], Cell[8419, 337, 85, 2, 70, "Print", Evaluatable->False], Cell[8507, 341, 85, 2, 70, "Print", Evaluatable->False], Cell[8595, 345, 85, 2, 70, "Print", Evaluatable->False], Cell[8683, 349, 85, 2, 70, "Print", Evaluatable->False], Cell[8771, 353, 85, 2, 70, "Print", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[8888, 357, 91, 2, 70, "Section", Evaluatable->False], Cell[CellGroupData[{ Cell[9002, 361, 136, 3, 70, "Subsection", Evaluatable->False, InitializationCell->True], Cell[9141, 366, 202, 4, 70, "Input", InitializationCell->True], Cell[9346, 372, 228, 5, 70, "Input", InitializationCell->True], Cell[9577, 379, 191, 4, 70, "Input", InitializationCell->True], Cell[9771, 385, 196, 4, 70, "Input", InitializationCell->True] }, Open ]], Cell[CellGroupData[{ Cell[9999, 391, 120, 3, 70, "Subsection", Evaluatable->False, InitializationCell->True], Cell[10122, 396, 227, 5, 70, "Input", InitializationCell->True], Cell[10352, 403, 668, 10, 70, "Input", InitializationCell->True] }, Open ]], Cell[CellGroupData[{ Cell[11052, 415, 92, 2, 70, "Subsection", Evaluatable->False], Cell[11147, 419, 215, 4, 70, "Input", InitializationCell->True], Cell[11365, 425, 693, 11, 70, "Input", InitializationCell->True], Cell[12061, 438, 693, 10, 70, "Input", InitializationCell->True] }, Open ]] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)