Corrigés des séances de TP de FLIN101
Expressions et Affectations
multiple:=proc(a::integer, b::integer)::boolean; return evalb(a mod b = 0); end proc;
Algorithmes, Procédures, Conditionnelles
triangleEquilateral:=proc(a::integer, b::integer, c::integer)::boolean; return (a = b) and (b = c); end proc;
triangle:=proc(a::integer,b::integer,c::integer)::boolean; return (a < b+c) and (b < a+c) and (c < a+b); end proc;
triangleIsocele:=proc(a::integer,b::integer,c::integer)::boolean; return (a = b) or (a = c) or (b = c); end proc;
triangleStrictementIsocele:=proc (a::integer, b::integer, c::integer)::boolean; return ((a = b) and a <> c) or ((a = c) and (a <> b)) or ((b = c) and (b <> a)); end proc;
ouExclusif:=proc(a::boolean,b::boolean)::boolean; return evalb(evalb(a) <> evalb(b)); end proc;
mul2ou3:=proc(n::integer)::boolean; return ouExclusif(multiple(n,2),multiple(n,3)); end proc;
Instructions Conditionnelles
memeDizaine:=proc(a::integer,b::integer)::boolean; return evalb(iquo(a,10) = iquo(b,10)); end proc;
memeParite:=proc(a::integer,b::integer)::boolean; return evalb(irem(a,2) = irem(b,2)); end proc;
gainRoulette:=proc(mise::integer,numJoue::integer,numSorti::integer)::integer; if (numJoue = numSorti) then return 20*mise; elif (memeDizaine(numJoue,numSorti)) then return 5*mise; elif (memeParite(numJoue, numSorti)) then return 2*mise; else return 0; end if; end proc;
gainRoulette2:=proc(mise::integer,numJoue::integer,numSorti::integer)::integer; local gain::integer; gain := 0; if (numJoue = numSorti) then gain := gain + 20*mise; end if; if (memeDizaine(numJoue, numSorti)) then gain := gain + 5*mise; end if; if (memeParite(numJoue, numSorti)) then gain := gain + 2*mise; end if; return gain; end proc;
gainRoulette2:=proc(mise::integer,numJoue::integer,numSorti::integer)::integer; local gain::integer; gain := 0; if (numJoue = numSorti) then gain := gain + 20*mise; end if; if (memeDizaine(numJoue, numSorti)) then gain := gain + 5*mise; end if; if (memeParite(numJoue, numSorti)) then gain := gain + 2*mise; end if; return gain; end proc;
Itération
somPair:=proc(n::integer)::integer; local somme::integer, i::integer; somme := 0; for i to n do; somme := somme+2*i; end do; return somme; end proc;
somImpair:=proc(n::integer)::integer; local somme::integer,i::integer; somme := 0; for i to n do somme := somme+2*i-1; end do; return somme; end proc;
emeMult:=proc(n::integer)::integer; local indice::integer,i::integer,resultat::integer; indice := 0; i := 1; while (indice < n) do; if (ouExclusif(multiple(i,2),multiple(i,3))) then indice := indice+1; resultat := i; end if; i := i+1; end do; return resultat; end proc;
Méthode Monte Carlo
dansCercle:=proc(x::float,y::float)::boolean; return evalb(x^2+y^2 < 1); end proc;
monteCarlo:=proc(n::integer)::float; local x::float,y::float,Nint::integer,i::integer; Nint := 0; for i to n do; x := coordalea(); y := coordalea(); if dansCercle(x,y) then Nint := Nint+1; end if; end do; return evalf((Nint/n)*4); end proc;
Algorithmes Sur Les Tableaux
tabEgaux:=proc(t::array,u::array)::boolean; local i::integer; if taille(t) <> taille(u) then return false; end if; for i to taille(t) do; if (t[i] <> u[i]) then return false; end if; end do; return true; end proc;
tabCarres:=proc(n::integer)::array(integer); local i::integer, t::array; t := array(1 .. n); for i to n do; t[i] := i^2; end do; return eval(t); end proc;
tabSuite:=proc (n::integer)::array(integer); local i::integer, t::array; t := array(1 .. n); t[1] := 1; for i from 2 to n do; t[i] := t[i-1]+2*(i-1)+1; end do; return eval(t); end proc;
tabMaxLocal:=proc (t::(array(integer)))::integer; local count::integer,i::integer; count := 0; if taille(t) < 2 then return taille(t); end if; if t[2] < t[1] then count := count+1; end if; for i from 2 to taille(t)-1 do; if t[i-1] < t[i] and t[i+1] < t[i] then count := count+1; end if; end do; if t[taille(t)-1] < t[taille(t)] then count := count+1; end if; return count; end proc;
nbChiffres:=proc(n::integer)::integer; return trunc(log[10](n))+1; end proc;
ecritureDecimale:=proc(n::integer)::array(integer); local t::array,i::integer,nb::integer; nb := nbChiffres(n); t := array(1..nb); for i to nb do; t[i] := irem(iquo(n,10^(nb-i)),10); end do; return eval(t); end proc;
ecritureBase:=proc(n::integer,k::integer)::array(integer); local t::array,i::integer,nb::integer; nb := trunc(log[k](n))+1; t := array(1..nb); for i to nb do; t[i] := irem(iquo(n,k^(nb-i)),k); end do; return eval(t); end proc;
Tri Par Insertion
insereTab:=proc(n::integer,T::array(integer))::array(integer); local NT::array,i::integer; NT := array(1..taille(T)+1); i := 1; if (taille(T) = 0) then return array([n]); end if; while ((i <= taille(T)) and (n > T[i])) do; NT[i] := T[i]; i:=i+1; end do; NT[i]:=n; while (i < taille(NT)) do; NT[i+1] := T[i]; i:=i+1; end do; return eval(NT); end proc;
triInsertion:=proc(T::array(integer))::array(integer); local NT::array,i::integer; if (taille(T) = 0) then return T; end if; NT:=array([T[1]]); i:=2; while (i<taille(T)) do; NT:=insereTab(T[i],NT); i:=i+1; end do; return eval(NT); end proc;
Listes
alea:=rand(1..20); Lex:=Vide; for i from 1 to 30 do; Lex := consL(alea(),Lex); end do;
DerListe:=proc(L::Liste)::integer; local L2::Liste; if EstVide(L) then; error "La liste est vide"; end if; L2:=L; while (not (EstVide(succ(L2)))) do; L2:=succ(L2); end do; return first(L2); end proc;
InverserListe:=proc(L::Liste)::Liste; local L2::Liste,LN::Liste; if EstVide(L) then return Vide; end if; L2:=L; LN:=Vide; while (not (EstVide(L2))) do; LN:=consL(first(L2),LN); L2:=succ(L2); end do; return LN; end proc;
ExtraireListe:=proc(L::Liste)::Liste; local L2::Liste,LN::Liste; if EstVide(L) then return Vide; end if; L2:=L; LN:=Vide; while (not (EstVide(L2))) do; if (isprime(first(L2))) then; LN:=consL(first(L2),LN); end if; L2:=succ(L2); end do; return InverserListe(LN); end proc;
somDiv:=proc(n::integer)::integer; local i::integer,c::integer,sqf::float,sqi::integer; c:=0; sqf:=evalf(sqrt(n)); sqi:=trunc(sqf); for i from 1 to sqi do; if (n mod i = 0) then; if (i^2 = n) then c:=c+i; else c:=c+i; c:=c+(n/i); end if; end if; end do; return c; end proc;
Nuage:=Vide; Droite:=Vide; for i from 40 to 1 by -1 do; Nuage:=consL([i,somDiv(i)],Nuage); end do; for i from 40 to 1 by -1 do; Droite:=consL([i,i+1],Droite); end do; plot([Nuage,Droite],colour=[red,blue]);
OccurListe:=proc(n::integer,L::Liste)::integer; local L2::Liste,c::integer; c:=0; L2:=L; while (not (EstVide(L2))) do; if (first(L2) = n) then c:=c+1; end if; L2:=succ(L2); end do; return c; end proc;
TabOccur:=proc(L::Liste)::array(integer); local i::integer,j::integer,T::array,L2::Liste; L2:=L; T:=array(1..20); for i from 1 to 20 do; T[i]:=0; end do; while (not (EstVide(L2))) do; j:=first(L2); T[j]:=T[j]+1; L2:=succ(L2); end do; return eval(T); end proc;
TriListe:=proc(L::Liste)::Liste; local i::integer,j::integer,LN::Liste,T::array; LN:=Vide; T:=array(1..20); T:=TabOccur(L); for i from 20 to 1 by -1 do; for j from 1 to T[i] do; LN:=consL(i,LN); end do; end do; return LN; end proc;
Concat:=proc(L1::Liste,L2::Liste)::Liste; local L2bis::Liste,LN::Liste; LN:=InverserListe(L1); L2bis:=L2; while (not (EstVide(L2bis))) do; LN:=consL(first(L2bis),LN); L2bis:=succ(L2bis); end do; return InverserListe(LN); end proc;
ConversionBase:=proc(L::Liste,b1::integer,b2::integer)::Liste; local L2::Liste,LN::Liste,n::integer,i::integer,k::integer,bx::integer,m::integer; LN:=Vide; L2:=InverserListe(L); n:=0;k:=0; while (not (EstVide(L2))) do; n:=n+first(L2)*(b1^k); k:=k+1; L2:=succ(L2); end do; m:=trunc(log[b2](n)); bx:=b2^m; for i from m to 0 by -1 do; LN:=consL(iquo(n,bx),LN); n:=n-(iquo(n,bx)*(b2^i)); bx:=iquo(bx,b2); end do; return InverserListe(LN); end proc;
Récursivité
expolent:=proc(a::float,b::integer)::float; if (b = 0) then return evalf(1); else return a * expolent(a,b-1); end if; end proc;
exporapide:=proc(a::float,b::integer,r::float)::float; if (b = 0) then return r; else return exporapide(a,b-1,a*r); end if; end proc;
concatR:=proc(L1::Liste,L2::Liste)::Liste; if (EstVide(L1)) then return L2; else return consL(first(L1),concatR(succ(L1),L2)); end if; end proc;
egalesL:=proc(L1::Liste,L2::Liste)::boolean; if EstVide(L1) then return EstVide(L2); elif EstVide(L2) then return EstVide(L1); else return evalb((first(L1) = first(L2)) and (egalesL(succ(L1),succ(L2)))); end if; end proc;
Des Lapins Et Des Lynx
-
