
PROGRAM ep96_406;

{
	Epreuve informatique de l'Ecole Polytechnique 96.406

La formule de Moivre exp(nix)=(cos x + i sin x)^n=cos nx + i sin nx
permet d'obtenir les relations suivantes :


cos 2x = 2 cosύx -1                      sin 2x = 2 sin x cos x

cos 3x = 4(cos x)^3 -3 cos x             sin 3x = sin x (4cosύ x -1)

cos 4x = 8(cos x)^8 -8cosύx +1           sin 4x =-4sin x((2cos x)^3-cos x)

et ainsi de suite ...

1/ Montrer que l'on peut exprimer les quantit‚s cos nx et (sin nx)/(sin x)
sous la forme de polyn“mes par rapport … cos x, que l'on notera
respectivement :  

               Pn(cos x)=cos nx     et   Qn(cos x)=(sin nx)/(sin x)

Ecrire un programme Pascal capable de calculer les coefficients de ces
polyn“mes pour des valeurs de n aussi ‚lev‚es que possible. 

Quelles sont les solutions des ‚quations Pn(x)=0 et Qn(x)=0 ?

2/ Tracer les graphes repr‚sentatifs des applications Pn(x) et Qn(x)

3/ A tout couple d'entiers (p,q) appartenant … Nύ on associe la fonction
r‚elle 
                      x
        f(p,q)(x)= Integrale [(sin y)^p (cos y)^q] dy
                     y=0

Ecrire un programme Pascal qui trace la courbe repr‚sentative de cette
fonction f(p,q)

4/ On pose  B(p,q)= f(p,q)(pi/2)
                                 pi/2
                  = f(p,q)(x)= Integrale [(sin y)^p (cos y)^q] dy
                                 y=0

Montrer par un raisonnement math‚matique que la valeur de  B(p,q) est de
la forme :

                B(p,q)= a + b.pi

o— a et b sont deux nombres rationnels.
 En d‚duire un programme Pascal qui donne la valeur exacte de a et b en
 fonction des entiers p et q.


:--------------------------------------------------------------:
:    Imprimer tous les r‚sultats                               :
:     en indiquant chaque fois … quoi ils correspondent        :
:--------------------------------------------------------------:
                            -=-=-=-

	
}


uses
  crt,modubase;

const
   maxpoints=99;
   maxn=3;
   degmax=99;

type
    MyReal=real;
    Intcoeff=Longint;
    IntPolynome=record
        c:array[0..degmax] of Intcoeff;
        deg:integer;
    end;
    frac=array [1..2] of Longint;

var
   ch:char;
   Car         : Char ;
   Question    : Char;

Function CoefficientQ(n,i:integer):Intcoeff; forward;

Function CoefficientP(n,i:integer):Intcoeff;
begin
     if i<0 then
        CoefficientP:=0
     else
         if n<2 then
            CoefficientP:=i
         else
             CoefficientP:=CoefficientP(n-1,i-1)
                          +CoefficientQ(n-1,i-2)
                          -CoefficientQ(n-1,i)
end;

Function CoefficientQ(n,i:integer):Intcoeff;
begin
     if (i<0) or (i>=n) then
        CoefficientQ:=0
     else
         if n=1 then
            CoefficientQ:=1
         else
             CoefficientQ:=CoefficientP(n-1,i)
                          +CoefficientQ(n-1,i-1);
end;


Procedure CalculeP(n:integer;VAR P:IntPolynome);
var
   i:integer;
begin
     P.deg:=n;
     With P do
          for i:=0 to deg do
              c[i]:=CoefficientP(n,i);
end;

Procedure CalculeQ(n:integer;VAR Q:IntPolynome);
var
   i:integer;
begin
     Q.deg:=n-1;
     With Q do
          for i:=0 to deg do
              c[i]:=CoefficientQ(n,i);
end;

Function ValeurIntPolynome(P:IntPolynome;x:MyReal):MyReal;
var
   i:integer;
   s,xn:MyREal;

begin
     s:=0;
     xn:=1;
     with P do
          for i:=0 to deg do
              begin
                   s:=s+c[i]*xn;
                   xn:=xn*x;
              end;
     ValeurIntPolynome:=s;

end;

Procedure AfficheIntCoeff(P:IntPolynome);
var
   i:integer;
begin
     With P do
          for i:=0 to deg do
               Write(c[i],' ');
end;

function Iq(x : real ; q : integer) : real;
begin
  if q=0 then
     Iq:=x
  else
      if q=1 then
         Iq:=sin(x)
       else
           Iq:=((q-1)*(Iq(x,q-2)+sin(x)*puiss(cos(x),q-1)))/q;
end;


function fpq(x:real;p,q:integer):real;
begin
  if p=0 then
     fpq:=Iq(x,q)
  else
      if p=1 then
         fpq:=(1-puiss(cos(x),q+1))/(q+1)
       else
           fpq:=(p-1)*fpq(x,p-2,q+2)/(q+1)
                -puiss(cos(x),q+1)*puiss(sin(x),p-1)/(q+1);
end;


procedure Iq2(q:integer ; var a,b : frac);
var
   c,d:frac;
begin
  if q=0 then
     begin
          a[1]:=0;a[2]:=1;
          b[1]:=1;b[2]:=2;
     end
  else
      if q=1 then
         begin
              a[1]:=1;a[2]:=1;
              b[1]:=0;b[2]:=1;
         end
       else
       begin
         iq2(q-2,c,d);
         a[1]:=(q-1)*c[1];
         a[2]:=q*c[2];
         b[1]:=(q-1)*d[1];
         b[2]:=q*d[2];
       end;
end;
procedure Simplifie(VAR a:frac);
var
   i:Longint;
begin
     i:=2;
     repeat
           if ((a[1] mod i) =0 ) and
              ((a[2] mod i) =0 ) then
                     begin
                          a[1]:=a[1] div i;
                          a[2]:=a[2] div i;
                     end
           else
               Inc(i)
     until (i*i)>a[2];
end;

procedure Bpq(p,q:integer;VAR a,b:frac);
var c,d : frac;
begin
  if p=0 then
     iq2(q,a,b)
  else
      if p=1 then
             begin
                  a[1]:=1;a[2]:=q+1;
                  b[1]:=0;b[2]:=1;
             end
       else
       begin
         bpq(p-2,q+2,c,d);
         a[1]:=(p-1)*c[1];
         a[2]:=(q+1)*c[2];
         b[1]:=(p-1)*d[1];
         b[2]:=(q+1)*d[2];
         Simplifie(a);
         Simplifie(b);

       end;
end;

Procedure Presentation;
begin
    ModeTexte;
    Efface;
    WriteLN('Formul de Moivre        ');
    WriteLN('                                    ');
    WriteLN('                                    ');
    WriteLN(' (1)   Calcul des polyn“mes Pn et Qn    ');
    WriteLN(' (2)   Trac‚ de  Pn(x)   et Qn(x)      ');
    WriteLN(' (3)   Graphe de f(p,q)(x)          ');
    WriteLN(' (4)   Calcul de B(p,q)              ');

    WriteLN('                           ');
    WriteLN('                           ');

    InitGraphique;
    ModeTexte;
end;


procedure Question1;
var
   i,n:integer;
   P:IntPolynome;
   x:MyReal;
begin
     ModeTexte;
     Efface;
     Writeln('ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ Question nψ1 ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»');
     Writeln('Ί                                                                   Ί');
     Writeln('Ί  (1)    Calcul des coefficients de P et Q                         Ί');
     Writeln('Ί                                                                   Ί');
     Writeln('ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ');

     n:=2;
     repeat
           Inc(n);
           WriteLN('n=',n);
           CalculeP(n,P);
           Write('Coefficients de P :  [0..',n,']:');
           AfficheIntCoeff(P);                          
           WriteLn;
           CalculeQ(n,P);
           Write('Coefficients de Q :  [0..',n,']:');
           AfficheIntCoeff(P);
           WriTeLN;
           PAuse;


     until false;
end;

procedure Question2;

Const
     xmin=-1.1;
     xmax=1.1;
     ymin=-2;
     ymax= 5;
     pas=0.01;
var
   P,Q:IntPolynome;
        n,c:integer;
        x:MyReal;

begin
     ModeTexte;
     Efface;
     Writeln('ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ Question nψ2 ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»');
     Writeln('Ί                                                                   Ί');
     Writeln('Ί  (2)    Trac‚ de  Pn(x)   et Qn(x)                                Ί');
     Writeln('Ί                                                                   Ί');
     Writeln('ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ');

     n:=0;

     ModeGraphique;
     repeat
           Inc(n);
           Efface;
           Couleur(Vert);
           Fenetre(xmin,xmax,ymin,ymax);
           X_Axe(0,0,1);
           Y_Axe(0,0,1);
           Deplace(0.1,1);Ecris('n=');EcrisEntier(n);
           Couleur(Jaune);
           CalculeP(n,P);
           x:=-1;
           Deplace(x,ValeurIntPolynome(P,x));
           repeat
                 Trace(x,ValeurIntPolynome(P,x));
                 x:=x+pas
            until x>1;

           Couleur(Blanc);
           CalculeQ(n,Q);
           x:=-1;
           Deplace(x,ValeurIntPolynome(Q,x));
           repeat
                 Trace(x,ValeurIntPolynome(Q,x));
                 x:=x+pas
           until x>1;
           Pause;
     until false;
end;




procedure Question3;
const
     xmin=-0.1;
     xmax=1.1;
     ymin=-0.1;
     ymax=3;
     pas=0.0001/pi;
var
   p,q:integer;
   x:Myreal;
begin
     Efface;
     Writeln('ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ Question nψ3 ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»');
     Writeln('Ί                                                                   Ί');
     Writeln('Ί   Graphe de f(p,q)(x)                                                 Ί');
     Writeln('Ί                                                                   Ί');
     Writeln('ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ');


     ModeGraphique;
     repeat
           efface;
           Efface;
           Couleur(Vert);
           Fenetre(xmin,xmax,ymin,ymax);
           X_Axe(0,0,1);
           Y_Axe(0,0,1);
           Deplace(0.1,ymax*0.8);
           p:=Random(3);
           q:=Random(3);
           Ecris(' p=');EcrisEntier(p);
           Ecris(' q=');EcrisEntier(q);
           Couleur(Jaune);

           x:=0;
           Deplace(x,fpq(x,p,q));
           repeat
                 Trace(x,fpq(x,p,q));
                 x:=x+pas;
           until x>pi/2;


           Pause;
     until false;

end;
                                                  

procedure Question4;
var
   p,q:integer;
   a,b:frac;
procedure Writefrac(a:frac);
begin
     write(a[1]);
     if a[2]<>0 then write('/',a[2]);
end;

begin
     Efface;
     Writeln('ΙΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ Question nψ4 ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»');
     Writeln('Ί                                                                   Ί');
     Writeln('Ί   Calcul de B(p,q)                                                Ί');
     Writeln('Ί                                                                   Ί');
     Writeln('ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ');

     repeat
           p:=random(9);
           q:=random(9);
           bpq(p,q,a,b);
           Write('B(',p,',',q,')=');
           if a[1]<>0 then Writefrac(a);
           if b[1]<>0 then
              begin
                   Write('PI*');
                   WriteFrac(b);
              end;
           bpq(q,p,a,b);
           Write('  B(',q,',',p,')=');
           if a[1]<>0 then Writefrac(a);
           if b[1]<>0 then
              begin
                   Write('PI*');
                   WriteFrac(b);
              end;
           WriTeLN;
           pause;

     until false;
end;


begin
  Randomize;
  Initgraphique;
    while true do
  begin
    Presentation;
     write('Question choisie ? ');
     readln(question);
     case question of
          '1':  Question1;
          '2':  Question2;
          '3':  Question3;
          '4':  Question4;
     end;
     Pause;
  end;
end.