Math/CSE 1560 Lab 7
Exercise: Writing while and for-loops in Maple
Objective
The objective of this exercise is to test your familiarity with solving problems
using while and for-loops in Maple.
Grading
This lab exercise is worth 10% of the course. It is meant to be completed in the
lab, individually (Collaboration is NOT allowed for this exercise).
NOTE: This is a 2 hour lab. The submission server will close shortly after 4:30
pm. You will not be able to submit your work after that time. No printout
s will be accepted.
Starting off:
- Open Maple 12 by clicking on the icon marked as such. If Maple does
not start, let the TA know immediately. If asked, choose to start a new workshe
et.
- On the first 2 lines, enter your name and student number as
comments -- start the lines with character #. Then save the file as
`lab7.mw` by clicking on `File` and then `save as`.
Problems
Note: Some of these problems can be solved using seq() or map(), For this assign
ment you must use for-loops instead.
- (4 points)
(4 points) Write a procedure that takes a sorted list of integers as input and returns the number of distinct elements in the list. You are NOT allowed to convert the list to a set for this question.
Test your procedure with lists in ascending order as well as lists in descending order.
- (4 points)
Perfect numbers: A number is defined to be perfect if it is equal to the sum of all its factors less than itself. E.g. 6 is a perfect number because its factors less than itself are 1,2,3 and 1+2+3=6.
Write a procedure that takes a positive integer n as input and returns all perfect numbers less than or equal to n. Feel free to use the solutions to previous assignments/exams (for finding lists of factors), but you may not use commands not covered in class.
- (5 points)
Write a procedure that takes a list with positive integers between 1 and
100 and returns a list of size 10 containing the counts in the ranges 1-10,11-2
0,21-30,..,91-100.
For example, when called on the list [11,21,31,41,51,53] it should return
[0,1,1,1,1,2,0,0,0,0]. Feel free to use the ceil function in this question.
- (4 points)
Write a procedure that takes as input a list and returns a list that is formed by retaining the elements that are prime and replacing each of the other elements with the next biggest prime number. For example if the input is [2,2,3,4,6,26] the output should be [2,2,3,5,7,29]. Do not use any Maple functions not covered in class.
- (3 points) Write a procedure that takes as input 2 positive integers, and generates the list of factors for each input (this was covered before in the course - feel free to use that code). Then compute the intersection of the two lists and return the highest number in the intersection (note that this is the gcd/hcf of the 2 imputs).
Final steps
- Save your worksheet.
- Submit the assignment as lab 7 in Moodle (the URL is moodle.math.yo
rku.ca). You can upload several times, but remember to submit using the "send fo
r marking" at the end ONCE - otherwise it may not be sent.
- You are done with this assignment. Remember to logout before you le
ave the lab.