Computer Science APEA 15-100, Summer 2009
Lab 7


Read these instructions first!


Note:  You may use loops and conditionals now.  You may not use Java concepts we have not yet covered, including arrays or methods from any classes in java.util.* to solve these problems.  Also, you may only use the charAt and length methods from the String class.  While they may be helpful, every problem here is solvable without them.


Also note:  For each of these problems, you should write your own tests!


  1. Hw6 solutions
    1. pi(n) -- the Prime Counting Function
    2. isPalindrome
    3. mostFrequentLetter
    4. isPalindrome Bonus
    5. Empirically Confirming the Prime Number Theorem
       
  2. sumOfDigits
    Write a method that takes an integer and returns the sum of its digits (in base 10).  For example, sumOfDigits(123) should return 6, as should sumOfDigits(-400020).
     
  3. isSubstring
    Write a method that takes two strings and returns true if and only if the first string is a substring of the second string.  For example, isSubstring("lid","holiday") returns true, but isSubstring("hay", "holiday") returns false.
     
  4. Confirm the following math facts:
    1. 12 + ... + n2                              = n(n+1)(2n+1)/6
    2. 13 + ... + n3                              = (1 + ... + n)2
    3. 1/3 + 1/9 + 1/27 + ...            = 1/2
    4. 1/12 + 1/22 + ...                    = π2/6
    5. 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...  = π/4
    6. 1/0! + 1/1! + 1/2! + ...           = e
    7. 1 - x2/2! + x4/4! - x6/6! + ... = cos(x)  [ x in radians ]

Carpe diem!