Category Archives: Assignment

Conditionals: 1000 digit Number

 

screen-shot-2016-10-27-at-8-36-29-am

Project Euler Problem 8

Largest product in a series
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Write a java program, ThirteenAdj_YI.java to find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

NOTE: Here is the code to handle the 1000-digit number

String adjStrNumbers = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
System.out.println("The length of this number is " + adjStrNumbers.length());
//The length of this number is 1000

Check what this code snippet does:

for ( int i = 0; i < 20; i++)
System.out.print( Integer.parseInt( adjStrNumbers.substring(i,i+1) ) );


WARNING:
The largest product of thirteen adjacent digits is 23514624000 if you declare the variables for product and largest of type “long”. Otherwise, when using type “int”, the largest product of thirteen adjacent digits is 2091059712 if you use int for x and product!!!!!!!!! THIS IS THE WRONG ANSWER

Conditionals: Relative prime numbers

Relative prime numbers

Write a program, RelativelyPrime_YI.java to prompt the user for an integer, n. The program displays an n-by-n table such a that there is an * in row i and column j if the gcd of i and j is 1 ( i and j are relatively prime) and a space in that position otherwise.

screen-shot-2016-10-17-at-11-16-58-am

Hint: use GCD_YI.java to finds the greatest common divisor of two integers

Example output:

>run ca_relativelyprime 2 20
   1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 
 1 * * * * * * * * * * * * * * * * * * * * 
 2 *   *   *   *   *   *   *   *   *   *   
 3 * *   * *   * *   * *   * *   * *   * * 
 4 *   *   *   *   *   *   *   *   *   *   
 5 * * * *   * * * *   * * * *   * * * *   
 6 *       *   *       *   *       *   *   
 7 * * * * * *   * * * * * *   * * * * * * 
 8 *   *   *   *   *   *   *   *   *   *   
 9 * *   * *   * *   * *   * *   * *   * * 
10 *   *       *   *   *   *       *   *   
11 * * * * * * * * * *   * * * * * * * * * 
12 *       *   *       *   *       *   *   
13 * * * * * * * * * * * *   * * * * * * * 
14 *   *   *       *   *   *   *   *   *   
15 * *   *     * *     *   * *   * *   *   
16 *   *   *   *   *   *   *   *   *   *   
17 * * * * * * * * * * * * * * * *   * * * 
18 *       *   *       *   *       *   *   
19 * * * * * * * * * * * * * * * * * *   * 
20 *   *       *   *   *   *       *   *   
> 
*/

Conditionals: Monty Hall

monty-hall

Assignment:
Game simulation.
In the game show Let’s Make a Deal, a contestant is presented with three doors. Behind one door is a valuable prize, behind the other two are gag gifts. After the contestant chooses a door, the host opens up one of the other two doors (never revealing the prize, of course).

The contestant is then given the opportunity to switch to the other unopened door. Should the contestant do so? Intuitively, it might seem that the contestant’s initial choice door and the other unopened door are equally likely to contain the prize, so there would be no incentive to switch. Write a program MonteHall_YI.java to test this intuition by simulation.

Your program should take an integer command-line argument n, play the game n times using each of the two strategies (switch or don’t switch) and print the chance of success for each strategy.

End the program with a message stating the conclusion: Is a bigger chance of winning if you switch to a different door?

Conditionals: Gambler: Modified Version

 

Modify Gambler.java to take an extra command line parameter that specifies the number of bets the gambler is willing to make so that there are three possible ways for the game to end:
* the gambler wins, loses, or runs “out of time” meaning the gambler gives up.
* Add to the output to give the expected amount of money the gambler will have when the game ends.

Think of the different outcomes and how would report on them. That means what your output would look like and how you would display it. Look back to the format of the previous gambler’s ruin activity.

A random(get it?) example:

stake: $5 goal: $10 trials: 10 games and run out of time is 7 bets

1 game – lost all your money
2 game – won
3 game – end by the 7th bet and you had $6
4 game – lost all your money
5 game – end by the 7th bet and you had $4
6 game – lost all your money
7 game – won
8 game – end by the 7th bet and you had $2
9 game – won
10 game – lost all your money

6 + 4 + 2 = 12 total accumulated from “run out of time”
the expected amount of money from 10 games is 12/3 = $4

Lesson slides on page 28

public class Gambler
{
    public static void main(String[] args)
   {
     int stake = Integer.parseInt(args[0]);
     int goal = Integer.parseInt(args[1]);
     int trials = Integer.parseInt(args[2]);

    int wins = 0;
    for (int i = 0; i < trials; i++)
     {
       int t = stake;
       while (t > 0 && t < goal)
        {
            if (Math.random() < 0.5) t++;
            else t--;
        }
        if (t == goal) wins++;
     }
   StdOut.println(wins + " wins of " + trials);  
   }
}

Conditionals: Gambler: Chart

WARNING: your program should run only once and produce this chart:
a. Modify the original gambler to show how the increase of “trials” affects the probability of winning.

A good easy format:
stake = 5 goal = 25

Trials Wins Probability
100 15%
1000 19%
10000 22%
100000 20%
1000000 20%

WARNING: your program should run only once and produce this chart:
b. Modify the original gambler to show how the increase of the “stake” affects the probability of winning.

A good easy format:
goal = 25 trials = 1000

Stake Wins Probability
5 17%
10 39%
15
20

WARNING: your program should run only once and produce this chart:
c. Modify the original gambler to show how the increase of the “goal” affects the probability of winning.

A good easy format:
stake = 5 trials = 1000

Goal Wins Probability
5 98%
10 50%
15
20
25

Conditionals: Project Euler: Sum Square Difference

Sum square difference

Problem 6

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

/** the correct output  is 25164150 **/

Conditionals: Gambler: Questions

From page 28 on this pdf:
1. What is the “if (t == goal) wins++; ” keeping track of?
2. What does the outer loop control?
3. What happens at each trial?
4. What is the identifier “win” for?
5. What is the “stake” for?
6. What is the “goal” for?
7. How many trials would produce a good result?
8. How do you read the output in plain English?
% java Gambler 5 25 1000
203 wins of 1000

  1. If you were running this program to find the probability of winning in this game, what would need to be done to come up with the best approximation to the actual value?

a. Implement another loop that will increase the goal by doubling it at every iteration and report the conclusion. Keep all other variables fixed. The stake should be $5.

b. Implement another loop that will increase the trials by factors of 10 it at every iteration and report the conclusion. Keep all other variables fixed. The stake should be $5.

Challenge (Optional):

Add what is needed to be able to systematically increase both the goals and the trials. Report your result in full a sentence.

public class Gambler
{
    public static void main(String[] args)
   {
     int stake = Integer.parseInt(args[0]);
     int goal = Integer.parseInt(args[1]);
     int trials = Integer.parseInt(args[2]);

    int wins = 0;
    for (int i = 0; i < trials; i++)
     {
       int t = stake;
       while (t > 0 && t < goal)
        {
            if (Math.random() < 0.5) t++;
            else t--;
        }
        if (t == goal) wins++;
     }
   StdOut.println(wins + " wins of " + trials);  
   }
}

Input/Output: StdIn and StdOut for Filtering Data – stat

Assignment:
Extend program Stats.java shown below to create a filter that prints all the values that are further than 1.5 standard deviations from the mean. FilterStats_YI.java

Stats.java takes an integer N from the command line, reads N double values from standard input, and prints their mean (average value) and sample standard deviation (square root of the sum of the squares of their differences from the average, divided by N – 1).

[spoiler title=’Stats.java’]

Below is the syntax highlighted version of Stats.java from §1.5 Input and Output.


/******************************************************************************
 *  Compilation:  javac Stats.java
 *  Execution:    java Stats n
 *  Dependencies: StdIn.java StdOut.java
 *  
 *  Reads in a command-line integer n, a sequence of n real numbers from
 *  standard input, and prints the mean and sample standard deviation.
 *
 *  % java Stats 6
 *  10.0 5.0 6.0
 *  3.0 7.0 32.0
 *  
 *  Mean                      = 10.5
 *  Sample standard deviation = 10.784247771634329
 *
 *  Note  signifies the end of file on Unix.
 *  On windows use .
 *
 ******************************************************************************/

public class Stats { 
    public static void main(String[] args) { 
        int n = Integer.parseInt(args[0]);
        double[] a = new double[n];

        // read data and compute statistics
        for (int i = 0; i < n; i++) {
            a[i] = StdIn.readDouble();
        }

        // compute mean
        double sum = 0.0;
        for (int i = 0; i < n; i++) {
            sum += a[i];
        }
        double mean = sum / n;

        // compute standard deviation
        double sum2 = 0.0;
        for (int i = 0; i < n; i++) {
            sum2 += (a[i] - mean) * (a[i] - mean);
        }
        double stddev = Math.sqrt(sum2 / (n - 1));

        // print results
        StdOut.println("Mean                      = " + mean);
        StdOut.println("Sample standard deviation = " + stddev);
    }
}

Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne. Last updated: Fri Oct 20 14:12:12 EDT 2017. [/spoiler]