Math/CSE 1560 Lab 9

Exercise: Recursive procedures in Maple

Objective
The objective of this exercise is to test your familiarity with solving problems using recursion in Maple.

Grading
This lab exercise 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 printouts will be accepted.

Starting off:

  1. 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 worksheet.
  2. On the first 2 lines, enter your name and student number as comments -- start the lines with character #. Then save the file as `lab9.mw` by clicking on `File` and then `save as`.

Problems
Note: Some of these problems can be solved using loops or seq() or map(). To get credit for this assignment you must use recursion instead.

  1. (5 points) Write a procedure using recursion that takes as input a positive integer and returns true if the input is a power of 2 and false otherwise. You are not allowed to use loops, the log function, or any function not covered in class.
  2. (5 points) Write a procedure recnextprime using recursion that takes as input a positive integer and returns the NEXT largest prime number. E.g. recnextprime(3) should return 5.
    You are not sllowed to use loops, the nextprime function, or any function not covered in class.
  3. (5 points) Write a procedure using recursion that takes as input a list and returns true if all the elements of the list are of type integer and false otherwise.
  4. (5 points) Write a procedure using recursion that takes as input a list L. It should print an error message if the list is of odd length; otherwise it returns the sum abs(L[1]-L[2]) +abs(L[3]-L[4]) +...+abs(L[nops(L)-1] -L[nops(L)]). You are not allowed to use loops.
    E.g. if L=[6,5,1,2,3,5] it should return 1+1+2=4.

Final steps

  1. Save your worksheet.
  2. Submit the assignment as lab 8 in Moodle (the URL is moodle.math.yorku.ca). You can upload several times, but remember to submit using the "send for marking" at the end ONCE - otherwise it may not be sent.
  3. You are done with this assignment. Remember to logout before you leave the lab.