3
SCHOOL OF MATHEMATICAL SCIENCES MTH3051 Introduction to Computational Mathematics Assignment 1. Long Multiplication. Due date: Lab class for week 4 This assignment contains just one question worth 25 marks (equal to 6% of the final mark for this unit). Late submissions will be subject to late penalties (see the unit guide for full details). 1. Multiplication If you were to ask any primary school student to compute 342 times 837 using long multiplication you could reasonably expect that they might produce a table somewhat like the following 342 × 837 2394 + 10260 + 273600 = 286254 The layout is simple. The first two lines are the integers to be multiplied. The next three lines are the results of multiplying each digit of the first number by successive digits of the second number. The final line equals the sum of the previous three lines. All of the calculations are done from right to left. Each column in the table is meant to record just one decimal digit and yet on occasions we obtain a two digit number (2 by 7 = 14, 6 plus 6 = 12). In such cases we carry the extra digits to the next column (2 by 7 = 14, write down 4 and carry the 1). But there is no reason to invoke the carry as soon as it arises, in fact all of the carries can be deferred to the very end. This allows us to write the multiplication of 342 by 837 as two tables, the first records the successive pairs of two digit multiplications

Stuff

Embed Size (px)

DESCRIPTION

Be Uploaded

Citation preview

  • SCHOOL OF MATHEMATICAL SCIENCES

    MTH3051

    Introduction to Computational Mathematics

    Assignment 1. Long Multiplication.

    Due date: Lab class for week 4

    This assignment contains just one question worth 25 marks (equal to 6% of the finalmark for this unit).

    Late submissions will be subject to late penalties (see the unit guide for fulldetails).

    1. Multiplication

    If you were to ask any primary school student to compute 342 times 837 using longmultiplication you could reasonably expect that they might produce a table somewhatlike the following

    342

    8372394

    + 10260

    + 273600

    = 286254

    The layout is simple. The first two lines are the integers to be multiplied. The nextthree lines are the results of multiplying each digit of the first number by successivedigits of the second number. The final line equals the sum of the previous three lines.All of the calculations are done from right to left.

    Each column in the table is meant to record just one decimal digit and yet on occasionswe obtain a two digit number (2 by 7 = 14, 6 plus 6 = 12). In such cases we carrythe extra digits to the next column (2 by 7 = 14, write down 4 and carry the 1). Butthere is no reason to invoke the carry as soon as it arises, in fact all of the carries canbe deferred to the very end. This allows us to write the multiplication of 342 by 837 astwo tables, the first records the successive pairs of two digit multiplications

  • School of Mathematical Sciences Monash University

    3 4 2

    8 3 721 28 14

    + 9 12 6

    + 24 32 16

    = 24 41 49 34 14

    followed by the carry process

    24 41 49 34 14

    = 24 41 49 35 4

    = 24 41 52 5 4

    = 24 46 2 5 4

    = 28 6 2 5 4

    = 2 8 6 2 5 4

    Your job is to write a single Matlab function called multiply. Here is a skeleton example

    function xy = multiply (x,y)

    %

    % This function computes the product of two positive integers, x and y

    % The result is returned as another integer xy.

    %

    % Usage: xy = multiply (x,y);

    % Input: x and y positive integers stored as row vectors

    % Output: xy the positive integer (row vector) equal to x times y

    ... Your code goes here ...

    Here is a simple example of how you would use your multiply function

    Matlab >> x = [1,2,3,4,5]; % first integer

    Matlab >> y = [6,7,8]; % second integer

    Matlab >> z = multiply(x,y); % do the long multiply

    Setting up the row vectors for x and y is a bit tedious so I have written a small Matlabprogram called decimaldigits.m (which you can find on the Moodle web page). Usingthat program, the above example would now be just

    Matlab >> x = decimaldigits(12345); % first integer

    Matlab >> y = decimaldigits(678); % second integer

    Matlab >> z = multiply(x,y); % do the long multiply

    Note that this decimaldigits function only works for integers less than about 16 digits.So if you really need to multiply very long numbers you may have to create the rowvectors for x and y by hand (as in the first example above).

    16-Feb-2014 2

  • School of Mathematical Sciences Monash University

    Ive also written another Matlab function factorial.m (also on Moodle). This functioncalls the multiply function to compute the factorial of any integer. Youll find that thenumber of digits in the factorial blows out very quickly and looks really messy whendisplayed as a row vector on the screen. To make the output look a bit more tidy Ivewritten one final Matlab function longprint.m. This prints the integers in a compactformat (and you can specify how many digits to print per line). Here is an example

    Matlab >> fact = factorial(100); % compute 100!

    Matlab >> longprint(fact,30); % print 30 digits per line

    Matlab >> % can combine the functions to compute and print 63! by 47!

    Matlab >> longprint(multiply(factorial(63),factorial(47)),40);

    2. What to submit

    When submitting the assignment you should include

    F A clear explanation of how your algorithm works (this could be your re-wordingof the introduction given on the assignment sheet, you do not need to explain thelines in your code that should be evident from the comments in the code),

    F Printouts of your Matlab multiply function.

    F Printouts of the answers for 100! and 63! times 47!

    Marks will be awarded for correctness as well as elegance (in particular how well youdocument the working of your Matlab programs. It should be easy for any body toread and understand your program. So use well chosen comments, but not so detailedthat it becomes hard to find the code!). But, and this is important, do not rely onthe comments in your Matlab code as the primary place in which you describe youralgorithm. There should be a separate discussion of the algorithm including any newequations you derived. This discussion should clearly describe your algorithm.

    Marks will be lost for poorly written programs, lack of clear and meaningfulcomments, and for an inadequate discussion.

    16-Feb-2014 3