import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;

public class kok_Legendre
{
public static double P(int n, double x)
{
if(n==0) return 1.0;
else if(n==1) return x;
else
{return (P((n-1),x)*x*(double)(2*n-1)-P((n-2),x)*(double)(n-1))/(double)n;}
// end legendre polynomials
}

//Derivative of Legendre Polynomial
public static double dP(int n, double x)
{
	double tt=0;
	for(int nn=n-1;nn>=0;nn--)
	{tt+=2.0*P(nn,x)/Math.sqrt(2.0/(2.0*nn+1));}
{return tt;}
// end legendre polynomials
}
public static double x0(int n,int k)
{
 double xx=(1.0-1.0/(8.0*n*n)+1.0/(1.0*n*n*n))*Math.cos(Math.PI*((4.0*k-1.0)/(2.0*n+2.0)));
 System.out.println("xx="+xx);
 return xx;
}

public static double kok_Legendre(int n,int k)
{
int nmax=10;
//double tolerance=1.0e-8;
double fx,dfx;
double x=x0(n,k);
for(int i=0;i<nmax;i++)
{
  fx=P(n,x);
  dfx=dP(n,x);
  x-=fx/dfx;
  //if(Math.abs(fx)<tolerance) { return x;}
}
//JOptionPane.showMessageDialog(null,"Uyarı maximum iterasyon sayısı aşıldı \n"+
//   " çözüm geçerli olmıyabilir","MAKSİMUM ITERASYON SAYISI UYARISI",JOptionPane.WARNING_MESSAGE);
return x;
}

     public static void main (String args[])  
     {
	 int n=10;
	 for(int k=0;k<10;k++)
	 {System.out.println("n="+n+"k="+k+"x="+kok_Legendre(n,k));}
     } 
}