KM01.mw

> restart;

Exercice 2: Moyenne Arithmétique de trois nombres a,b,c

> moyenne:=proc(a,b,c)
local m;

m:=(a+b+c)/3;

return(m);

end;

moyenne := proc (a, b, c) local m; m := 1/3*a+1/3*b+1/3*c; return m end proc

>

> moyenne(1,2,3);

2

>

> restart;

Exercice 4: Racines d'un polynôme du second degré

> racines:=proc(a,b,c)
local delta,x_1,x_2,sol;

delta:=b^2-4*a*c;

if is(delta>=0) then x_1:=(-b-sqrt(delta))/(2*a);

                     x_2:=(-b+sqrt(delta))/(2*a);

                     sol:={x_1,x_2};

               else sol:={};

fi;

return(sol);

end;        

racines := proc (a, b, c) local delta, x_1, x_2, sol; delta := b^2-4*a*c; if is(0 <= delta) then x_1 := 1/2*(-b-sqrt(delta))/a; x_2 := 1/2*(-b+sqrt(delta))/a; sol := {x_1, x_2} else sol := {} end if; ...

> racines(1,-2,1);

{1}

> racines(1,-5,6);

{2, 3}

> racines(1,0,1);

{}

Exercice 5: fonction solve

> solve(a*x^2+b*x+c=0,x);

1/2*(-b+(b^2-4*a*c)^(1/2))/a, -1/2*(b+(b^2-4*a*c)^(1/2))/a

> solve(x^2-2*x+1=0,x);

1, 1

> solve(x^2-5*x+6=0,x);

3, 2

> solve(x^2+1=0,x);

I, -I

>

> restart;

Exercice 7: Boucle conditionnelle

> test:=proc(M)
local k,somme;

k:=0;somme:=0;

while (somme<=M) do

                    k:=k+1;

                    somme:=somme+1/k;

                od;

return(k);

end;

test := proc (M) local k, somme; k := 0; somme := 0; while somme <= M do k := k+1; somme := somme+1/k end do; return k end proc

> test(0);

1

> test(1);

2

> test(10);

12367

>

>

> restart;

Exercice 8: nombres pairs croissant

> for i from 0 to 20 by 2 do
                         print(i);

                        od;        

0

2

4

6

8

10

12

14

16

18

20

Exercice 9: nombres impairs décroissant

> for i from 19 to 1 by -2 do
                         print(i);

                        od;

19

17

15

13

11

9

7

5

3

1

>

> restart;

Exercice 10

> f:=proc(x)
local sol;

if is(x>0) then sol:=exp(-1/x);

           else sol:=0;

fi;

return(sol);

end;

>

f := proc (x) local sol; if is(0 < x) then sol := exp(-1/x) else sol := 0 end if; return sol end proc

> plot(f);

[Plot]

> (3<4)and(2<3);

> restart;

true

Exercice 11

> g:=proc(x)
local sol;

if is(x<-1) then sol:=exp(2*x+2);

elif (is(x>=-1))and(is(x<=0)) then sol:=2*x+3;

elif is(x>0) then sol:=x^2+2*x+3;

fi;

return(sol);

end;

>

g := proc (x) local sol; if is(x < -1) then sol := exp(2*x+2) elif is(-1 <= x) and is(x <= 0) then sol := 2*x+3 elif is(0 < x) then sol := x^2+2*x+3 end if; return sol end proc

> g(Pi);

Pi^2+2*Pi+3

> plot(g,-2..1);

[Plot]

> g_bis:=x->piecewise(is(x<-1),exp(2*x+2),(is(x>=-1))and(is(x<=0)),2*x+3,is(x>0),x^2+2*x+3);

g_bis := proc (x) options operator, arrow; piecewise(is(x < -1), exp(2*x+2), is(-1 <= x) and is(x <= 0), 2*x+3, is(0 < x), x^2+2*x+3) end proc

> g_bis(Pi);plot(g_bis,-2..1);

Pi^2+2*Pi+3

[Plot]

>

> restart;

>

> Exercice 12

> somme:=0:

> for i from 1 to 100 do somme:=somme+i; od:

> somme;

5050

> sum(k,k=1..100);

5050

>

> restart;

Exercice 13 (Méthode 1)

> compteur:=0:

> for i from 3 to 1000 by 3 do compteur:=compteur+1; od:

> compteur;

333

>

> restart;

Exercice 13 (Méthode 1)

> compteur:=0: i:=1:

> while i<1000 do i:=i+1; if (i mod 3)=0 then compteur:=compteur+1; fi; od:

> compteur;

333

>

> restart;

Exercice 14

> i:=0: prod:=1:

> while is(prod<=10^9) do i:=i+5: prod:=prod*i: end:

> sol:=i-5;

sol := 35

> p35:=product(5*k,k=1..7);

p35 := 393750000

> p40:=product(5*k,k=1..8);

p40 := 15750000000

> is(p35<=10^9);

true

> is(p40<=10^9);

false

>

>