 # Java Programming Problems

## Question

Problem 1. (Ramanujan’s Taxi) Srinivasa Ramanujan was an Indian mathematician who became famous for his intuition for numbers. When the English mathematician G. H. Hardy came to visit him one day, Hardy remarked that the number of his taxi was 1729, a rather dull number. To which Ramanujan replied, “No, Hardy! It is a very interesting number. It is the smallest number expressible as the sum of two cubes in two diﬀerent ways.” Verify this claim by writing a program Ramanujan that takes a command-line argument N and prints out all integers less than or equal to N that can be expressed as the sum of two cubes in two diﬀerent ways. In other words, ﬁnd distinct positive integers a, b, c, and d such that a3 + b3 = c3 + d3. Hint: Use four nested for loops and 3 √N as the upper bound for the loop variables. You have to be clever about your choice of lower bounds for the loop variables, or else you will see duplicate entries in your output.
\$ java Ramanujan 10000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3

Problem 2. (Counting Primes) Write a program PrimeCounter that takes a commandline argument N and prints the number of primes less than or equal to N. Note that if you are not careful about the upper bound of the loop that tests for the primality of a number, your program may not ﬁnish in a reasonable amount of time! Hint: Use the algorithm (p. 70) from of the text to test if a number is prime.
\$ java PrimeCounter 100
25

Problem 3. (Exponential Function) Write a program Exp that takes two command-line arguments x and n, and prints the value of ex calculated using the ﬁrst n terms of the formula e^x = 1/ 0! + x /1! + x^2/ 2! + x^3/ 3! + ....
\$ java Exp 1 20
2.7182818284590455

Problem 4. (Sine Function) Write a program Sin that takes two command-line arguments x (in degrees) and n, and prints the value of sin(x) calculated using the ﬁrst n terms of the formula sin(x) = x/ 1! – x^3/ 3! + x^5/ 5! – x^7/ 7! + ....
\$ java Sin 45 20
0.7071067811865475

Problem 5. (Cosine Function) Write a program Cos that takes two command-line arguments x (in degrees) and n, and prints the value of cos(x) calculated using the ﬁrst n terms of the formula cos(x) = 1 0! − x2 2! + x4 4! − x6 6! + .... Hint: Use an approach similar to the one you used for Problem 3.
\$ java Cos 45 20
0.7071067811865475

Problem 6. (Minesweeper) Write a program Minesweeper that takes three commandline arguments M, N, and p and: produces an M-by-N boolean array where each entry is occupied (true) with probability p — in the minesweeper game, occupied cells represent bombs and empty cells represent safe cells; prints out the array using an asterisk for bombs and a period for safe cells; and prints out the array with each safe square replaced with the number of neighboring bombs (above, below, left, right, or diagonal). Try to write your code so that you have as few special cases as possible to deal with, by using an (M + 2)-by-(N + 2) boolean array.
\$ java Minesweeper 10 10 0.4
. . . * * * * . . . .
* . * . * * . * .
. . * . . * . . * .
. * . . . . * . . .
. . . . * . * . . *
. . . * . . * . . .
. . . * * * * . . .
* . * * * . . . . *
. . . . . * . . .
. . . * * . * . * . *
1 1 3 * * * * 3 1 1 1
* 4 * 6 * * 4 * 2
2 3 * 2 3 * 4 4 * 2
1 * 2 2 2 4 * 3 2 2
1 1 2 2 * 4 * 3 1 *
0 0 2 * 5 6 * 3 1 1
1 2 4 * * * * 2 1 1
* 2 * * * 5 3 1 1 *
1 3 4 5 5 * 3 1 3 2
0 1 * * 3 * 3 * 2 *

Problem 7. (Birthday Problem) Suppose that people enter an empty room until a pair of people share a birthday. On average, how many people will have to enter before there is a match? Write a program Birthday that takes a command-line argument T and runs T experiments to estimate this quantity. Assume birthdays to be uniform random integers between 0 and 364.
\$ java Birthday 1000
24

## Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class Ramanujan {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

if (args.length != 1) {
System.out.println("Sample: java Ramanujan 10000 \n");
System.exit(0);
}

try {
int n = Integer.parseInt(args);
int a,b,c,d;
int t1, t2;
int t3;
for (int i = 1; i <= n; i++) {
a = b = c = d = 0;
for (int j = 1; j < i; j++) {
t1 = (int) Math.pow(j, 3);
t2 = 0;
// k should bigger than j to avoid duplication
// k < i - t1 to reduce number of looping step
for (int k = j + 1; k < i - t1; k++) {
t2 = (int) Math.pow(k, 3);
t3 = t1 + t2;
if ( t3 == i) { // match
if (a == 0) { // first expression
a = j;
b = k;
} else {...
\$61.25 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

### Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.