previous | index | next

Another Example

Recall the definition of quotient:

quotient(n,d) = -quotient(n,-d) if d<0
= -quotient(-n,d) if n<0
= 0 if n<d
= 1 + quotient(n-d,d) otherwise

Racket C++
(define quotient
  (lambda (n d)
    (cond
      ((< d 0) 
       (- (quotient n (- d))))
      ((< n 0) 
       (- (quotient (- n) d)))
      ((< n d) 
       0)
      (else    
       (+ 1 (quotient (- n d) d))))))
int quotient(int n, int d) {
  if ( d < 0 ) 
    return -quotient(n, -d);
  else if ( n < 0 )
    return -quotient(-n, d);
  else if ( n < d )
    return 0;
  else
    return 1 + quotient(n-d, d);
}


previous | index | next