MTH3051 Introduction To Computational Mathematics Assignment 1. Long Multiplication
MTH3051 Introduction To Computational Mathematics Assignment 1. Long Multiplication
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
Monash University
21 28 14
9 12
6
+
+
24
32
16
24
41
49
24
24
24
24
28
41 49 34 14
41 49 35
4
41 52
5
4
46
2
5
4
6
2
5
4
34
14
=
=
=
=
=
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 >>
Matlab >>
Matlab >>
x = [1,2,3,4,5];
y = [6,7,8];
z = multiply(x,y);
% first integer
% second integer
% do the long multiply
Setting up the row vectors for x and y is a bit tedious so I have written a small Matlab
program called decimaldigits.m (which you can find on the Moodle web page). Using
that program, the above example would now be just
Matlab >>
Matlab >>
Matlab >>
x = decimaldigits(12345);
y = decimaldigits(678);
z = multiply(x,y);
% first integer
% second integer
% 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 row
vectors for x and y by hand (as in the first example above).
16-Feb-2014
Monash University
Ive also written another Matlab function factorial.m (also on Moodle). This function
calls the multiply function to compute the factorial of any integer. Youll find that the
number of digits in the factorial blows out very quickly and looks really messy when
displayed as a row vector on the screen. To make the output look a bit more tidy Ive
written one final Matlab function longprint.m. This prints the integers in a compact
format (and you can specify how many digits to print per line). Here is an example
Matlab >>
Matlab >>
fact = factorial(100);
longprint(fact,30);
% compute 100!
% print 30 digits per line
Matlab >>
Matlab >>
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-wording
of the introduction given on the assignment sheet, you do not need to explain the
lines in your code that should be evident from the comments in the code),
Marks will be awarded for correctness as well as elegance (in particular how well you
document the working of your Matlab programs. It should be easy for any body to
read and understand your program. So use well chosen comments, but not so detailed
that it becomes hard to find the code!). But, and this is important, do not rely on
the comments in your Matlab code as the primary place in which you describe your
algorithm. There should be a separate discussion of the algorithm including any new
equations you derived. This discussion should clearly describe your algorithm.
Marks will be lost for poorly written programs, lack of clear and meaningful
comments, and for an inadequate discussion.
16-Feb-2014