Home

Project 9

Task 1

Write these functions in the VM language:

// returns a if a>b, otherwise returns b
int max(a, b)
// returns a/b; assume a>=0 and b>=0
int quotient(a, b)
// returns a^b; assume a>=0, b>=0; computes iteratively;
// uses mult; note, n^0 = 1
int iterativePow(a, b)
// returns a^b; assume a>=0, b>=0; computes recursively;
// uses mult; note, n^0 = 1
int recursivePow(a, b)
// returns -1 if n is prime, 0 otherwise;
// uses mod function from notes; assume n>=2
int prime(n)

Download test files: project-09.zip. The test files include working implementations of examples from class.

Task 2

Write these “words” in Forth. The code must run on Gforth.

max2 ( a b -- maxval // must not use builtin max )
quotient ( a b -- a/b // must not use builtin / or mod )
prime? ( n -- -1/0 )
CSCI 201 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.