Exercises 8.5

 

1.     10 cubed = 1000

9 cubed = 729

8 cubed = 512

7 cubed = 343

6 cubed = 216

5 cubed = 125

4 cubed = 64

3 cubed = 27

2 cubed = 8

1 cubed = 1

 

 

2.     10 squared = 100

8 squared = 64

6 squared = 36

4 squared = 16

2 squared = 4

 

 

3.

    1

1

2

2

3

3

1

4

4

2

5

5

3

1

 

 

 

4.

    3

1

3

5

 

 


5.

 

3 1 3

3 1 2

3 1 1

3 2 3

3 2 2

3 3 3

2 1 2

2 1 1

2 2 2

1 1 1

 

 

6.   

1 1 1

 

1 2 1

1 2 2

 

1 3 1

1 3 2

1 3 3

 

 

2 2 2

 

2 3 2

2 3 3

 

 

 

3 3 3

 

 

7.    

      1 1 1

      1 1 2

      1 2 1

      1 2 2

      1 2 3

      1 3 1

      1 3 2

      1 3 3

      1 3 4

      2 1 1

      2 1 2

      2 1 3

      2 2 1

      2 2 2

      2 2 3

      2 2 4

      2 3 1

      2 3 2

      2 3 3

      2 3 4

 

 

      1 1 1

      1 1 2

      1 1 3

      1 1 4

      1 2 1

      1 2 2

      1 2 3

      1 2 4

      1 3 1

      1 3 2

      1 3 3

      1 3 4

      2 1 1

      2 1 2

      2 1 3

      2 1 4

      2 2 1

      2 2 2

      2 2 3

      2 2 4

      2 3 1

      2 3 2

      2 3 3

      2 3 4

 

 

8.

      1 1 1

      1 1 2

      1 1 3

      1 1 4

      1 2 1

      1 2 2

      1 2 3

      1 2 4

      1 3 1

      1 3 2

      1 3 3

      1 3 4

      2 1 1

      2 1 2

      2 1 3

      2 1 4

      2 2 1

      2 2 2

      2 2 3

      2 2 4

      2 3 1

      2 3 2

      2 3 3

      2 3 4

 


9.    

1 1 1

1 1 2

1 1 3

1 1 4

1 2 1

1 2 2

1 2 3

1 2 4

1 3 1

1 3 2

1 3 3

1 3 4

2 2 1

2 2 2

2 2 3

2 2 4

2 3 1

2 3 2

2 3 3

2 3 4

 

 

10.

2 1 1

 

 

 

11.

      1 1 1

      1 1 2

1 1 3

1 1 4

1 2 1

1 2 2

1 2 3

1 2 4

1 3 1

1 3 2

1 3 3

1 3 4

2 1 1

2 1 2

2 1 3

2 1 4

2 2 1

2 2 2

2 2 3

2 2 4

2 3 1

2 3 2

2 3 3

2 3 4

 


12.

5 1 9

4 2 6

3 3 3

2 4 0

1 5 -3

 

13.

0 10 10

1 9 11

2 8 12

3 7 13

4 6 14

5 5 15

6 4 16

 

14.

0 10 10

1 9 11

2 8 12

3 7 13

4 6 14

5 5 15

6 4 16

7 3 17

8 2 18

9 1 19

10 0 20

11 -1 21

 

15.

5

1

2

3

###

3

1

###

1

***

 

16.

base-2 log of 32 = 5

base-2 log of 16 = 4

base-2 log of 8 = 3

base-2 log of 4 = 2

base-2 log of 2 = 1

base-2 log of 1 = 0

 


17.

2

3

5

9

4

6

10

6

9

 

*****

 

18.

0 1

1 -1

2 3

 

 

19.

      0002448612*****

 

 

20.                   While loop                            Do-while loop                                    Forever loop

 

number

product

 

number

product

 

number

product

 

0

1

 

0

1

 

0

1

 

1

1

 

1

1

 

1

1

 

2

2

 

2

2

 

2

2

 

3

6

 

3

6

 

3

6

 

4

24

 

4

24

 

4

24

 

 

 

 

 

 

 

5

24

 

21.                   While loop                               Do-while loop                      Forever loop

 

number

product

 

number

product

 

number

product

 

0

1

 

0

1

 

0

1

 

1

1

 

1

1

 

1

1

 

2

2

 

2

2

 

2

2

 

 

 

 

 

 

 

3

2

 

22.                   While loop                               Do-while loop                      Forever loop

 

number

product

 

number

product

 

number

product

 

0

1

 

0

1

 

0

1

 

 

 

 

1

1

 

1

1

 

 

 

 

 

 

 

2

2

 

 

 

 

 

 

 

3

2

 

 


23.

      do

      {

            theScreen.println(x);

            x -= 0.5;

      }

      while (x > 0);

 

 

24.

      for (int i = 1; i <= 50 ; i++)

         theScreen.println( (2*i) * (2*i) );

 

25.

      for (double x = 1.0; x <= 5.0; x += 0.25)

            theScreen.println( Math.sqrt(x) );

 

26.

      int fib1 = 1, fib2 = 1, fib3;

      theScreen.println(1);

      theScreen.println(1);

 

      for (;;)

      {

         fib3 = fib1 + fib2;

         if (fib3 >= 500) break;

         theScreen.println(fib3);

         fib1 = fib2;

         fib2 = fib3;

      }

27.

      for(;;)

      {

         theScreen.print("Enter a real number: ");

         x = theKeyboard.readDouble();

         if (x > 0) break;

 

         theScreen.println("Sorry, you entered: " + x

                              + "\nThe number must be POSITIVE");

      }

 

 

28.

public static int summation (int first, int last)

{

   int runningTotal = 0;

 

   for (int count = first; count <= last; count++)

      runningTotal += count;

 

   return runningTotal;

}

 

 


29.

public static BigInteger summation (BigInteger n)

{

   BigInteger runningTotal = BigInteger.ZERO,

                     count = BigInteger.ONE;

 

   while ( count.compareTo(n) <= 0)

   {

            runningTotal = runningTotal.add(count) ;

            count = count.add( BigInteger.ONE );

   }

 

   return runningTotal;

}

 

 

30.

public static BigInteger summation (BigInteger first,

                                    BigInteger last)

{

   BigInteger runningTotal = BigInteger.ZERO,

                    count = first;

 

   while ( count.compareTo(last) <= 0)

   {

            runningTotal = runningTotal.add(count) ;

            count = count.add( BigInteger.ONE );

   }

 

   return runningTotal;

}

 

 

31.

public static double Power(double x, int n)

 {

         double product = 1.0;

         for (int i = 0; i < n; i++)

            product *= x;

         return product;

 }

 

 

32.

public static double power(double x, int n)

 {

   double product = 1.0;

   for (int i = 0; i < Math.abs(n); i++)

      product *= x;

   if (n >= 0)

      return product;

   else if (x != 0)

      return 1.0 / product;


   else

   {

      System.err.println("0 to a negative power is undefined Ð "

                       + "returning 0\n");

      return 0;

   }

 }

 

33.

public static int divisorSum (int n)

{

   int sum = 0;

   for (int i = 1; i <= n/2; i++)

      if (n % i == 0) sum += i;

   return sum;

}

 

34.

public static boolean isPrime(int n)

{

    if (n < 2)

       return false;                      // 0 & 1 are not primes

 

    if (n == 2)

       return true;                       // 2 is prime

 

    if (n % 2 == 0)

       return false;                      // evens > 2 are not prime

 

for (int d = 3; d*d <= n; d++)      // check if any odd integer // in the range 3 through

// square root of n divides n

       if (n % d == 0)

          return false;                   // d divides n; n not prime

 

    return true;                          // otherwise, n is prime

}

 

35.

public static int intLog2(int n)

{

   int count = 0;

   while (n > 1)

   {

      count++;

      n /= 2;

   }

   return count;

}