Java Reference
In-Depth Information
We then repeat starting with each successive prime number but no elements are
removed since there are no multiples of 5 or 7 (a more efficient implementation
of the algorithm would stop without examining 5 or 7). The numbers that
remain in the list are all prime numbers.
Implement this algorithm using an ArrayList of Integers that is initialized to the
values from 2 to 100. Your program can iterate numerically through the ArrayList
from index 0 to index size( )-1 to get the current prime number, but should use an
Iterator to scan through the remainder of the list to eliminate the multiples. You
can use the listIterator method to retrieve the iterator starting at a specified index
into the ArrayList . Output all remaining prime numbers to the console.
3.
The birthday paradox is that there is a surprisingly high probability that two or
more people in the same room happen to share the same birthday. By birthday,
we mean the same day of the year (ignoring leap years), but not the exact birth-
day that includes the birth year or time of day. Write a program that approxi-
mates the probability that two or more people in the same room have the same
birthday, for 2 to 50 people in the room.
The program should use simulation to approximate the answer. Over many trials
(say, 5000), randomly assign birthdays (i.e., the number 1-365) to everyone in the
room. Use a HashSet to store the birthdays. As the birthdays are randomly generated
use the contains method of a HashSet to see if someone with the same birthday is
already in the room. If so, increment a counter that tracks how many times at least
two people have the same birthday and then move on to the next trial. After the trials
are over divide the counter by the number of trials to get an estimated probability
that two or more people share the same birthday for a given room size.
Your output should look something like the following. It won't be exactly the
same due to the random numbers:
For 2 people, the probability of two birthdays is about 0.002
For 3 people, the probability of two birthdays is about 0.0082
For 4 people, the probability of two birthdays is about 0.0163
...
For 49 people, the probability of two birthdays is about 0.9654
For 50 people, the probability of two birthdays is about 0.969
Note: To generate a random number x , where 0
x < 1, use x = Math.random( ) ; .
This generates a random number between 0 and 1.
4.
The textfiles boynames.txt and girlnames.txt , which are included in the source
code for this topic, contain lists of the 1000 most popular boy and girl names in the
United States for the year 2005, as compiled by the Social Security Administration.
These are blank-delimited files where the most popular name is listed first,
the second most popular name is listed second, and so on to the 1000th most
popular name, which is listed last. Each line consists of the first name followed
by a blank space followed by the number of registered births in the year using
that name. For example the girlnames.txt file begins with:
Emily 25494
Emma 22532
Search WWH ::




Custom Search