package utilities;


import java.awt.*;

public class InvT {
	static double StatCom(double q, int i, int j, int b) {
	    double zz=1; 
	    double z=zz; 
	    int k=i; 
	    while(k<=j) { 
	    	zz=zz*q*k/(double)(k-b); 
	    	z=z+zz; 
	    	k=k+2;
	    }
    	return z;
    }

	static double StudT(double t, int n) {
	    t=Math.abs(t); 
	    double w=t/Math.sqrt(n); 
	    double th=Math.atan(w);
	    if(n==1) { return 1-th/(Math.PI/2.0); }
	    
	    double sth=Math.sin(th); 
	    double cth=Math.cos(th);
	    if((n%2)==1)
			return 1-(th+sth*cth*StatCom(cth*cth,2,n-3,-1))/(Math.PI/2.0);
	    else
			return 1-sth*StatCom(cth*cth,1,n-3,-1);
    }


	static double AStudT(double p,int n) { 
		double v=0.5; 
		double dv=0.5; 
		double t=0;
		double val;
		
    	while(dv>1e-6) { 
    		t=1.0/v-1; 
    		dv=dv/2.0; 
    		val = StudT(t,n);
    		if(val>p) { 
    			v=v-dv;
    		} 
    		else { 
    			v=v+dv;
    		}
    	}
    	return t;
    }
}