Lab Assignment 5

  1. [solve] A proper factor of a positive integer n is a (positive) factor of n that is less than n. For example, 4 is a proper factor of 12. There are exactly three proper factors of 6. They are 1, 2 and 3.

    A positive integer is defined to be perfect if it is equal to the sum of its proper factors. For example, 6 is perfect since the proper factors of 6 are 1, 2 and 3, and 1 + 2 + 3 = 6. 8 is not perfect since the proper factors of 8 are 1, 2 and 4, and 1 + 2 + 4 ≠ 8. Mathematicians look at perfect numbers because there is a connection between them and certain prime numbers.

    1. Write a definition of a function isPerfect(n) that yields true if n is perfect and false if not. Use a loop to accumulate the sum of the proper factors of n, then check whether that sum is equal to n.

    2. Add an Execute part that displays the first three perfect numbers. Use a loop. Be sure to count how many numbers have been displayed. You are supposed to display the first three perfect numbers, not the perfect numbers that are less than or equal to three.

      Use your isPerfect function to test whether a number is perfect. So you will find yourself writing something like

        If isPerfect(n) then
      
      inside a loop, where you display n and add 1 to your count if n is perfect.

  2. [solve] For this question, your goal is to write and test a definition of function replace(x, y, s), which replaces the first occurrence of string x by string y inside string s. For example,

    Notice that the string being replaced does not have to be a word. It can be anywhere, including in the middle of a word.

    If x does not occur in s at all, then replace(x, y, s) should yield s. For example,

     

    Functions often use other functions to help them. You will write a helper function that makes writing replace(x, y, s) easier.

    1. Write a definition of function indexOf(x, s), which yields the position where the first occurrence of x starts in s. If x does not occur in s, then indexOf(x, y) should yield 0. For example,

      • indexOf("bcd", "abcdefg") = 2 (since "bcd" occurs in "abcdefg" starting at the second character)

      • indexof("defg", "abcdefg") = 4 (since "defg" occurs in "abcdefg" starting at the fourth character)

      • indexof("cab", "abcdefg") = 0 (since "cab" does not occur in "abcdefg")

      Use a loop to search for the first position where x occurs in s. Notice that x occurs in s at position n if x == s_*[n, ..., n + length(x) - 1].

      Write an Execute part that tests your indexOf function. Try it on at least one case where the string occurs and one where it does not. Also test indexOf("a", "a"). When indexOf appears to work, continue to the next step.

    2. Write a definition of replace(x, y, s). Use indexOf to find the position where x occurs in s (or whether it occurs at all). Then build the result string by concatenating the part before the first occurrences of x with y and the part that occurs after the first occurrence of x. Think about how to find the parts.

      Do not compute the same thing more than once. To avoid computing something twice, give its answer a name. Then refer to that name.

    3. Write an Execute part that tests your replace function. Try it on at least one case where the string to be replaced does occur in the string, and at least one case where the string to be replaced does not occur in the string. Display the results.