MATH 211 Homework, Spring 2008
American University of Beirut · Discrete Structures · 2 pages
More MATH 211 files
Show the text of this homework
Math 211 - Spring 2006-2007
Maple Assignment 2
Due date: May 10, 2007
Important Instructions:
Important Instructions:
Important Instructions:
• Students are allowed to work in groups consisting of at most 3 students.
• Students will be interviewed based on which the assignment grades will
be finalized
• Assignment submissions should be on Moodle
• Penalty of 20 points per day after May, 10 2007
• If you have any question pass by bliss 206 Tuesday and Thursday anytime
between 11:00 and 2:00
Exercise 1.
Exercise 1.
Exercise 1. Write a procedure CompositeNumbers(n) which takes as input
an integer n and prints all the composite numbers less than n.
Test the procedure and save the results.
Exercise 2.
Exercise 2.
Exercise 2.
1. Write a procedure PrimeFactors1(n) which takes as input an integer n
and returns a set containing all the prime numbers which divide n.
For example:
• PrimeFactors1(10) should return 2, 5
• PrimeFactors1(396) should return 2, 3, 11
• PrimeFactors1(5940) should return 2, 3, 5, 11
2. Write a procedure PrimeFactors2(n,A) which takes as input an integer
n and a set A containing the result returned by PrimeFactors1(n) and
returns a new set containing the multiplicity of each of the elements of A.
For example:
1
• PrimeFactors2(10,PrimeFactors1(10)) should return 1, 1
since 10 = 21 ∗ 51
• PrimeFactors1(396,PrimeFactors1(396)) should return 2, 2, 1
since 396 = 22 ∗ 32 ∗ 111
• PrimeFactors1(5940,PrimeFactors1(5940)) should return 2, 3, 1, 1
since 5940 = 22 ∗ 32 ∗ 51 ∗ 111
3. Write a procedure myLCM(a,b) which takes as input two integers a and
b and returns their least common multiple by using PrimeFactors2.
4. Write a procedure myGCD(a,b) which takes as input two integers a and
b and returns their greatest common divisor by using PrimeFactors2.
5. Test each procedure and compare your results with the built-in gcd and
lcm functions of Maple.
Exercise 3.
Exercise 3.
Exercise 3. Let P(n) = P(n − 1) + n2 with P(1) = 1
1. Write an iterative procedure myIterativeEval(n) which takes as input
an integer n and returns the value of P(n).
2. Write a recursive procedure myRecursiveEval(n) which takes as input
an integer n and returns the value of P(n).
3. Test each procedure twice
Note.
Note.
Note. For the first two problems use the following functions of Maple:
1. isprime(n) - takes as input an integer n and returns true if n is prime and
false if not.
2. nextprime(n) - takes as input an integer n and returns the next largest
prime.
3. mod(a,b) - takes as input two integers and returns the remainder computed
upon dividing a by b.
GOOD LUCK ⌣
2