Exercice 1 - manipulations de base

> Max:=proc(x,y)
if x<y then return y;

else return x;

end if;

end proc;

Max := proc (x, y) if x < y then return y else return x end if end proc

> Max(6,7);

7

> Max(-1,2);

2

> Max(-1.5,-1.6);

-1.5

> Max3:=proc(x,y,z)
return Max(x,Max(y,z));

end proc;

Max3 := proc (x, y, z) return Max(x, Max(y, z)) end proc

> Max3(1,2,3);

3

> Max3(2,1,3);

3

> Max3(3,2,1);

3

> Min:=proc(x,y)
if x=Max(x,y) then return y;

else return x;

end if;

end proc;

Min := proc (x, y) if x = Max(x, y) then return y else return x end if end proc

> Min(3,4);

3

> Max(3,4);

4

>

Exercice 2 - quelques boucles

> TermeSuite:=proc(n::nonnegint)
local a,b,c,i;

if n=0 or n=1 then return 1;

end if;

a:=1; b:=1;

for i from 2 to n do

c := 3*a - b + 1;

b := a;

a := c;

end do;

end proc;

TermeSuite := proc (n::nonnegint) local a, b, c, i; if n = 0 or n = 1 then return 1 end if; a := 1; b := 1; for i from 2 to n do c := 3*a-b+1; b := a; a := c end do end proc

> TermeSuite(1);

1

> TermeSuite(3);

9

> TermeSuite(4);

25

> Fibo:=proc(n::nonnegint)
local a,b,i;

if n=0 then return 0;  

elif n=1 then return 1;

end if;

a:=1; b:=0;

for i from 2 to n do

a := a + b;

b := a - b;

end do;

return a;

end proc;

Fibo := proc (n::nonnegint) local a, b, i; if n = 0 then return 0 elif n = 1 then return 1 end if; a := 1; b := 0; for i from 2 to n do a := a+b; b := a-b end do; return a end proc

> Fibo(2);

1

> Fibo(3);

2

> Fibo(4);

3

> Fibo(5);

5

> Fibo:=proc(p::nonnegint, n::nonnegint)
local a,b,i;

if p=0 or n=p then return 1;  

elif p=1 then return n;

end if;

return Fibo(p-1,n-1)+Fibo(p,n-1);

end proc;

Fibo := proc (p::nonnegint, n::nonnegint) local a, b, i; if p = 0 or n = p then return 1 elif p = 1 then return n end if; return Fibo(p-1, n-1)+Fibo(p, n-1) end proc

> Fibo(1,5);

5

> Fibo(3,5);

10

> Fibo(12,22);

646646

> b:=Array(1..9,1..25);

b := RTABLE(548464, anything, Array, rectangular, Fortran_order, [], 2, 1 .. 9, 1 .. 25)

>

> Fibo2:=proc(p::nonnegint, n::nonnegint)
local a,i;

global b;

if p=0 or n=p then b[p,n]:=1; return 1; end if;

if b[p,n]<>0 then return b[p,n]; end if;

if p=1 then b[p,n]:=n;return n; end if;

b[p-1,n-1]:=Fibo(p-1,n-1);

b[p,n-1]:=Fibo(p,n-1);

b[p,n]:= b[p-1,n-1]+b[p,n-1];

return b[p,n];

end proc;

Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...Fibo2 := proc (p::nonnegint, n::nonnegint) local a, i; global b; if p = 0 or n = p then b[p, n] := 1; return 1 end if; if b[p, n] <> 0 then return b[p, n] end if; if p = 1 then b[p, n] := n; return n ...

> Fibo2(9,24);

1307504

> time(Fibo2(9,24));time(Fibo(9,24));

0.

6.799

> b;

RTABLE(554112, anything, Array, rectangular, Fortran_order, [], 2, 1 .. 6, 1 .. 15)

Exercice 3 - Décomposition binaire

> Puissance:=proc(x)
local n,q;

if (x <= 0) then printf("Valeur positive, svp...");

else

n:=0; q:=1;

while (x >= 2**n) do

n:= n+1;

end do;

return n-1, 2**(n-1);

end if;

end proc;

Puissance := proc (x) local n, q; if x <= 0 then printf(

> Puissance(0);

Valeur positive, svp...

> Puissance(1);

0, 1

> Puissance(4);

2, 4

> Puissance(9);

3, 8

> Puissance(35.5);

5, 32

> decomp:=proc(x)
local i,t,r,b;

t:=x;

i:=0;

while t>=1 do

r := irem(t,2,'t');

b[i] := r;

i:=i+1;

end do;

i:=i-1;

while i >= 0 do

printf("%d",b[i]);

i:=i-1;

end do;

end proc;


decomp := proc (x) local i, t, r, b; t := x; i := 0; while 1 <= t do r := irem(t, 2, 't'); b[i] := r; i := i+1 end do; i := i-1; while 0 <= i do printf(

> decomp(2);

10

-1

> decomp(7);

111

-1

> decomp(13);

1101

-1

>