Computer Science APEA 15-100, Summer 2009
Lab 7
Read these instructions first!
- The homework collaboration policy does not apply to labs. In
labs, you may work alone or in small groups. And you may show each
other your code, and help each other in any manner. This is
encouraged, in fact!
- That said, the participation policy is still in effect -- you must
not only attend labs, but actually work during the labs. If you have
completed the assigned work, then you should either help others, if they
request it, or you should delve deeper into the assigned material.
But you must use the lab time exclusively to explore the material covered in
that lab.
- In particular, you may not use lab time to do your homework.
- There is nothing to submit for labs.
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!
- Hw6 solutions
-
pi(n) -- the Prime Counting Function
-
isPalindrome
-
mostFrequentLetter
-
isPalindrome Bonus
-
Empirically Confirming the Prime Number Theorem
- 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).
- 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.
- Confirm the following math facts:
- 12 + ... + n2
= n(n+1)(2n+1)/6
- 13 + ... + n3
= (1 + ... + n)2
- 1/3 + 1/9 + 1/27 + ...
= 1/2
- 1/12 + 1/22 + ...
= π2/6
- 1 - 1/3 + 1/5 - 1/7 + 1/9 - ... =
π/4
- 1/0! + 1/1! + 1/2! + ...
= e
- 1 - x2/2! + x4/4! - x6/6! + ... =
cos(x) [ x in radians ]
Carpe diem!