| > | 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(1,2,3); |
| > |
| > | 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(1,-2,1); |
| > | racines(1,-5,6); |
| > | racines(1,0,1); |
Exercice 5: fonction solve
| > | solve(a*x^2+b*x+c=0,x); |
| > | solve(x^2-2*x+1=0,x); |
| > | solve(x^2-5*x+6=0,x); |
| > | solve(x^2+1=0,x); |
| > |
| > | 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(0); |
| > | test(1); |
| > | test(10); |
| > |
| > |
| > | restart; |
Exercice 8: nombres pairs croissant
| > | for i from 0 to 20 by 2 do
print(i); od; |
Exercice 9: nombres impairs décroissant
| > | for i from 19 to 1 by -2 do
print(i); od; |
| > |
| > | restart; |
Exercice 10
| > | f:=proc(x)
local sol; if is(x>0) then sol:=exp(-1/x); else sol:=0; fi; return(sol); end; |
| > |
| > | plot(f); |
![[Plot]](images/KM01_37.gif)
| > | (3<4)and(2<3); |
| > | restart; |
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(Pi); |
| > | plot(g,-2..1); |
![[Plot]](images/KM01_41.gif)
| > | 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(Pi);plot(g_bis,-2..1); |
![[Plot]](images/KM01_44.gif)
| > |
| > | restart; |
| > |
| > | Exercice 12 |
| > | somme:=0: |
| > | for i from 1 to 100 do somme:=somme+i; od: |
| > | somme; |
| > | sum(k,k=1..100); |
| > |
| > | restart; |
Exercice 13 (Méthode 1)
| > | compteur:=0: |
| > | for i from 3 to 1000 by 3 do compteur:=compteur+1; od: |
| > | compteur; |
| > |
| > | 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; |
| > |
| > | restart; |
Exercice 14
| > | i:=0: prod:=1: |
| > | while is(prod<=10^9) do i:=i+5: prod:=prod*i: end: |
| > | sol:=i-5; |
| > | p35:=product(5*k,k=1..7); |
| > | p40:=product(5*k,k=1..8); |
| > | is(p35<=10^9); |
| > | is(p40<=10^9); |
| > |
| > |