Small logo Working with Numbers

Problem Set 7 1 2 3 4

  1. A whole number a is said to divide a whole number b if and only if there is a whole number x such that b = a x. In symbols, we write "a b" to mean "a divides b" and "a  b" to mean "a does not divide b". For example,

    • 2 6 since 6 = 2 3

    • 6 2 since there is no whole number x such that 2 = 6 x

    • 6 0 since 0 = 6 0

    • 0 6 since there is no whole number x such that 6 = 0 x

    • 0 0 since 0 = 0 1776.

    1. Value: 10 points.
      The predicate function divides? inputs whole numbers a and b (in that order) and outputs the boolean #t if and only if a b. Using the Scheme Evaluator provided, write and test a definition for divides? that makes use of the function remainder. (Make sure that you deal with the cases when one or both of the inputs are zero.)

      Problem Set 7 (12a)

      • Your program:
        Values will appear here.
        Press to run some simple built-in test cases.
        Test results will appear here.

      Alert: The function divides? is a useful one to include in your Scheme library. Add it to your Numbers file. After doing so, return here and check that your library is up to date and functioning correctly:

      Press this button to test your Scheme library:
    2. Value: 5 points.
      There is a standard boolean Scheme function that, if given a whole number a, outputs the boolean #t if and only if a is an even number. Its name is even?. If such a primitive function didn't exist, however, it would be easy to define it. Using the Scheme Evaluator provided, write and test a definition for a one-input boolean function is-even? that makes use of the function divides? and behaves just like the primitive function even?. (Of course, your definition should not make use of the primitive function.)

      Problem Set 7 (12b)

      • Your program:
        Values will appear here.
        Press to run some simple built-in test cases.
        Test results will appear here.

      Once you have completed your work on this part of this problem, you should always use the primitive function even? if you want to check whether or not an integer is even.

  2. The factorial of a whole number n — which we denote by n! — may be defined as follows:

    n! =

    {

    1

     

    if n = 0

    n (n – 1)!

    if n 0.

    In words: For each whole number n, n! (read "n-factorial") is 1 if n is zero, or n times (n – 1)! otherwise. Thus, for example, 5! = 120:

    5!

    = 5 4!
    = 5 (4 3!)
    = 5 (4 (3 2!))
    = 5 (4 (3 (2 1!)))
    = 5 (4 (3 (2 (1 0!))))
    = 5 (4 (3 (2 (1 1))))
    = 120.

    Value: 10 points.
    Using the Scheme Evaluator provided, write and test a definition for the function factorial that inputs a whole number n and outputs n!.

    Problem Set 7 (13)

    • Your program:
      Values will appear here.
      Press to run some simple built-in test cases.
      Test results will appear here.

    Alert: The function factorial is a useful one to include in your Scheme library. Add it to your Numbers file. After doing so, return here and check that your library is up to date and functioning correctly:

    Press this button to test your Scheme library:
Previous pageNext page