// Arup Guha
// 11/5/2026
// Code that compares theoretical result from Fall 2026 COP 3503 Exam 2 Question 1 total
// a simulation.
import java.util.*;

public class e2q1 {

	public static void main(String[] args) {
	
		// Try 99 different values of p that are spaced out.
		for (double p=.01; p<=.995; p+=.01) {
			double experimental = avgPerTrial(100000, p);
			double theoretical = (2 - p)/p/p/p;
			System.out.printf("%.2f %.5f %.5f\n", p, experimental, theoretical);
		}
	
	}
	
	// Runs nTrials number of trials with probability of success p.
	public static double avgPerTrial(int nTrials, double p) {
	
		// Add up the total.
		double total = 0;
		for (int i=0; i<nTrials; i++)
			total += sim(p);
			
		// Here is the average.
		return total/nTrials;
	
	}

	// Returns total cost in thousands for one trial with prob p.
	public static int sim(double p) {
		
		// Res is the total cost.
		int res = 0;
		
		// Iter is which test number I am on.
		int iter = 1;
		while (true) {
			
			// Add my cost for this test.
			res += (iter*iter);
			
			// Take it.
			double tmp = Math.random();
			
			// Woohoo I passed, I don't spend any more!
			if (tmp <= p) break;
			
			// If we get here I failed!
			iter++;
		}
	
		return res;
	}
}