<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/** Demo for 15 March 2011 */
public class D {
    /** = number of weeks it takes to fill up apt --see p 244 */
    public static int roaches() { 
        double roachVol= .001;     // Space one roach takes 
        double aptVol= 20*20*8;  // Apartment volume 
        double growthRate= 1.25; // Population growth rate per week 
        
        int w= 0; // the week
        int pop= 100;  // population at week w
        // Increase w until the apt is filled.
        
        // inv: definition of pop, and apt not filled the week before
        while ( roachVol * pop &lt; aptVol) {
            pop= (int) (pop * (1 + growthRate));
            w= w +1;
        }
        
        // the apt is filled at week w
        // it is not filled the week before
        return w;
    } 
    
    /** = b** c. Precondition: c &gt;= 0 */
    public static double exp(double b, int c) {
        double z= 1;
        double x= b;
        double y= c;
        // invariant: z*x^y = b^c  and y &gt;= 0
        while (y&gt;0) {
            if (y%2 == 0) {
                x= x*x; y= y/2;
            } else {
                z= z*x; y= y-1;
            }
        }
        
        // z = b^c
        return z;
    }
    
    /** = b** c. Precondition: c &gt;= 0 */
    public static double expRec(double b, int c) {
        if (c == 0)
            return 1;
        if (c%2 == 0)
            return expRec(b*b, c/2);
        return b * expRec(b, c-1);
    }
    
    
    /** = quotient and remainder when x is divided by y.
          Precondition: x &gt;= 0 and y &gt; 0 */
    public static Wrap2 qr(int x, int y) {
        // Store remainder in r and quotient in q
        int r= x;
        int q= 0;
        // invariant: x = q*y + r and 0 &lt;= r
        while (r &gt;= y) {
            q= q+1;
            r= r-y;
        }
        
        // x = q*y + r and 0 &lt;= r &lt; y
        return new Wrap2(q, r);
        
    }
 
    
}</pre></body></html>