Lab Assignment 2

  1. [solve] Write a definition of function triple(x) so that triple(x) = 3x. Test it by pushing the Compute button. After it works, submit it by pushing the Submit button.

  2. [solve] Write a definition of function divisibleBy10(n) that yields true if n is divisible by 10 (and false if n is not divisible by 10). Test it by pushing the Compute button. After it works, submit it by pushing the Submit button.

  3. [solve] The sign of a number x is defined to be -1 if x < 0, to be 0 if x = 0 and to be 1 if x > 0. Write a definition of function sign(x), which yields the sign of x. Use cases.

    Test it by pushing the Compute button. After it works, submit it by pushing the Submit button.

  4. [solve] The greatest common factor of two nonnegative integers x and y is the largest integer that is a factor of both x and y. Calling this function gcf(x, y), you would expect to find that gcf(14,49) = 7 and gcf(25,50) = 25.

    We defined the greatest common factor function in class. The definition employs the following facts.

    1. gcf(0,y) = y
    2. gcf(x,y) = gcf(y `mod` x, x) (as long as x is not 0).
    Write the definition of gcf and test it by pushing the Compute button. After it works, submit it by pushing the Submit button.

  5. [solve] Write a definition of function powerOf2(n) so that powerOf2(n) = 2n. Assume that n is a nonnegative integer.

    For example, powerOf2(1) = 2 and powerOf2(4) = 16.

    For this problem, you are not allowed to use the ^ operator to compute powers. Instead, employ the idea of recursion. Notice the following two facts.

    1. 20 = 1
    2. 2n = 2(2n-1) (as long as n > 0).
    Test your definition by pushing the Compute button. After it works, submit it by pushing the Submit button.

  6. [solve] Write a function sumDigits(n) that produces the sum of the digits of a nonnegative integer n, when n is written in standard (base 10) notation. For example, sumDigits(24) = 6 since 2 + 4 = 6, and sumDigits(788) = 23 since 7 + 8 + 8 = 23. Use recursion.

    Notice that sumDigits(0) = 0. But what about a number that is greater than 0? Notice that sumDigits(12345) = sumDigits(1234) + 5. and sumDigits(788) = sumDigits(78) + 8. In general, for a positive integer n, add the rightmost digit of n to the result produced by sumDigits on the number that you get by removing the rightmost digit of n.

    Test your definition by pushing the Compute button. After it works, submit it by pushing the Submit button.

  7. [solve] When you write a number in base 10, each digit represents how many of a particular power of 10 are in the number. From right to left, the digits represent powers 100, 101, 102, ... For example 123 = 1×102 + 2×101 + 3×100. To emphasize that the base is 10, we will write 12310.

    Computers represent numbers in base 2, not base 10. In base 2, the digits (from right to left) represent powers 20 21, 22, 23, ... The digits in a number are only allowed to be 0 or 1. For example, 1012 = 1×22 + 0×21 + 1×20 = 510. The following table shows a few numbers written in base 10 (decimal) and base 2 (binary).

    Decimal    Binary
    1    1
    3    11
    4    100
    6    110
    10    1010
    15    1111
    16    10000

    This problem is similar to the preceding problem, but this time the idea is to count how many of the bits in a binary number are 1. Call the function numOneBits(n). For example, numOneBits(5) = 2 since 510 is written 1012 in base 2, and two of the digits are 1. Similarly, numOneBits(15) = 4 since 1510 is written 11112 in base 2.

    (Note. n `mod` 2 is the rightmost bit of n (written in binary) and n `div` 2 is what you get by removing the rightmost bit. For example, (11011112) `mod` 2 = 1 and (11011112) `div` 2 = (1101112).)

    Test your definition by pushing the Compute button. After it works, submit it by pushing the Submit button.