> 1 > restart; > Meilleur:=proc(t,N) > local grand,i; > grand:=t[0]; > for i from 1 to N-1 do > if t[i]>grand then > grand:=t[i] > fi; > od; > return grand; > end; Meilleur := proc(t, N) local grand, i; grand := t[0]; for i to N - 1 do if grand < t[i] then grand := t[i] end if end do; return grand end proc > t:=array(0..3,[1,4,4,3]); t := array(0 .. 3, [ (0) = 1 (1) = 4 (2) = 4 (3) = 3 ]) > Meilleur(t,4); 4 > DeuxMeilleursDistincts:=proc(t,N) > local grand,i; > grand:=array(0..1); > grand[0]:=t[0]; > grand[1]:=0; > for i from 1 to N-1 do > if t[i]>grand[0] then > grand[1]:=grand[0]; > grand[0]:=t[i] > elif t[i]>grand[1] and t[i] grand[1]:=t[i] > fi; > od; > return grand; > end; DeuxMeilleursDistincts := proc(t, N) local grand, i; grand := array(0 .. 1); grand[0] := t[0]; grand[1] := 0; for i to N - 1 do if grand[0] < t[i] then grand[1] := grand[0]; grand[0] := t[i] elif grand[1] < t[i] and t[i] < grand[0] then grand[1] := t[i] end if end do; return grand end proc > print(DeuxMeilleursDistincts(t,4)); array(0 .. 1, [ (0) = 4 (1) = 3 ]) > 2 > restart; > DecomposerBase:=proc(N,k) > local a,kclone,i; > a:=array(0..N-1); > kclone:=k; > for i from 0 to N-1 do > a[i]:=irem(kclone,N); > kclone:=iquo(kclone,N) > od; > return a; > end; DecomposerBase := proc(N, k) local a, kclone, i; a := array(0 .. N - 1); kclone := k; for i from 0 to N - 1 do a[i] := irem(kclone, N); kclone := iquo(kclone, N) end do; return a end proc > print(DecomposerBase(7,446)); array(0 .. 6, [ (0) = 5 (1) = 0 (2) = 2 (3) = 1 (4) = 0 (5) = 0 (6) = 0 ]) > CalculerNombre:=proc(N,a) > local k,i; > k:=a[N-1]; > for i from 2 to N do > k:=k*N+a[N-i] > od; > return k; > end; CalculerNombre := proc(N, a) local k, i; k := a[N - 1]; for i from 2 to N do k := k*N + a[N - i] end do; return k end proc > a:=DecomposerBase(7,446):CalculerNombre(7,a); 446 > 3 > restart; > nombreOccurrences:=proc(t,N,m) > local nbre,i; > nbre:=0; > for i from 0 to N-1 do > if t[i]=m then > nbre:=nbre+1 > fi; > od; > return nbre; > end; nombreOccurrences := proc(t, N, m) local nbre, i; nbre := 0; for i from 0 to N - 1 do if t[i] = m then nbre := nbre + 1 end if end do; return nbre end proc > t:=array(0..8,[1,1,2,0,1,2,1,2]):m:=1:nombreOccurrences(t,8,m); 4 > positionOccurrences:=proc(t,N,m) > local p,i,j; > p:=array(0..N-1); > for i from 0 to N-1 do > p[i]:=N > od; > j:=0; > for i from 0 to N-1 do > if t[i]=m then > p[j]:=i; > j:=j+1 > fi; > od; > return p; > end; positionOccurrences := proc(t, N, m) local p, i, j; p := array(0 .. N - 1); for i from 0 to N - 1 do p[i] := N end do; j := 0; for i from 0 to N - 1 do if t[i] = m then p[j] := i; j := j + 1 end if end do; return p end proc > print(positionOccurrences(t,8,m)); array(0 .. 7, [ (0) = 0 (1) = 1 (2) = 4 (3) = 6 (4) = 8 (5) = 8 (6) = 8 (7) = 8 ]) > nombreOccurrences_bis:=proc(t,N,m) > local nbre,long,i; > nbre:=0;long:=0; > for i from 0 to N-1 do > if t[i]=m[long] then > if long=1 then > nbre:=nbre+1; > long:=0 > else > long:=long+1 > fi; > fi; > od; > return nbre; > end; nombreOccurrences_bis := proc(t, N, m) local nbre, long, i; nbre := 0; long := 0; for i from 0 to N - 1 do if t[i] = m[long] then if long = 1 then nbre := nbre + 1; long := 0 else long := long + 1 end if end if end do; return nbre end proc > mbis:=array(0..1,[1,2]);nombreOccurrences_bis(t,8,mbis); mbis := array(0 .. 1, [ (0) = 1 (1) = 2 ]) 3 > positionOccurrences_bis:=proc(t,N,m) > local p,long,i,j; > p:=array(0..N-1); > for i from 0 to N-1 do > p[i]:=N > od; > j:=0; > long:=0; > for i from 0 to N-1 do > if t[i]=m[long] then > if long=1 then > p[j]:=i-1; > j:=j+1; > long:=0 > else > long:=long+1 > fi; > fi; > od; > return p; > end; positionOccurrences_bis := proc(t, N, m) local p, long, i, j; p := array(0 .. N - 1); for i from 0 to N - 1 do p[i] := N end do; j := 0; long := 0; for i from 0 to N - 1 do if t[i] = m[long] then if long = 1 then p[j] := i - 1; j := j + 1; long := 0 else long := long + 1 end if end if end do; return p end proc > print(positionOccurrences_bis(t,8,mbis)); array(0 .. 7, [ (0) = 1 (1) = 4 (2) = 6 (3) = 8 (4) = 8 (5) = 8 (6) = 8 (7) = 8 ]) > 4 > restart; > Scinder:=proc(u,p) > local ts,i; > ts:=array(0..1); > ts[0]:=array(0..2^(p-1)-1); > ts[1]:=array(0..2^(p-1)-1); > for i from 0 to 2^(p-1)-1 do > ts[0][i]:=u[i]; > ts[1][i]:=u[i+2^(p-1)] > od; > return ts; > end; Scinder := proc(u, p) local ts, i; ts := array(0 .. 1); ts[0] := array(0 .. 2^(p - 1) - 1); ts[1] := array(0 .. 2^(p - 1) - 1); for i from 0 to 2^(p - 1) - 1 do ts[0][i] := u[i]; ts[1][i] := u[i + 2^(p - 1)] end do; return ts end proc > u:=array(0..7,[25,4,0,12,9,4,8,0]):print(Scinder(u,3)); array(0 .. 1, [ (0) = array(0 .. 3, [ (0) = 25 (1) = 4 (2) = 0 (3) = 12 ]) (1) = array(0 .. 3, [ (0) = 9 (1) = 4 (2) = 8 (3) = 0 ]) ]) > Fusionner:=proc(t,s,p) > local u,it,is,iu,j; > u:=array(0..2^(p+1)-1); > it:=0; > is:=0; > iu:=0; > while it<2^p and is<2^p do > if t[it] u[iu]:=t[it]; > it:=it+1 > else > u[iu]:=s[is]; > is:=is+1 > fi; > iu:=iu+1; > od; > if it=2^p and iu<2^(p+1) then > for j from iu to 2^(p+1)-1 do > u[j]:=s[is+j-iu] > od; > elif is=2^p and iu<2^(p+1) then > for j from iu to 2^(p+1)-1 do > u[j]:=t[it+j-iu] > od; > fi; > return u; > end; Fusionner := proc(t, s, p) local u, it, is, iu, j; u := array(0 .. 2^(p + 1) - 1); it := 0; is := 0; iu := 0; while it < 2^p and is < 2^p do if t[it] < s[is] then u[iu] := t[it]; it := it + 1 else u[iu] := s[is]; is := is + 1 end if; iu := iu + 1 end do; if it = 2^p and iu < 2^(p + 1) then for j from iu to 2^(p + 1) - 1 do u[j] := s[is + j - iu] end do elif is = 2^p and iu < 2^(p + 1) then for j from iu to 2^(p + 1) - 1 do u[j] := t[it + j - iu] end do end if; return u end proc > a:=array(0..3,[2,4,4,9]);b:=array(0..3,[1,1,4,10]); > print(Fusionner(a,b,2)); a := array(0 .. 3, [ (0) = 2 (1) = 4 (2) = 4 (3) = 9 ]) b := array(0 .. 3, [ (0) = 1 (1) = 1 (2) = 4 (3) = 10 ]) array(0 .. 7, [ (0) = 1 (1) = 1 (2) = 2 (3) = 4 (4) = 4 (5) = 4 (6) = 9 (7) = 10 ]) > triFusion:=proc(t,p) > local tscinde,a,b; > if p=0 then > return t > else > tscinde:=Scinder(t,p); > a:=triFusion(tscinde[0],p-1); > b:=triFusion(tscinde[1],p-1); > return Fusionner(a,b,p-1) > fi; > end; triFusion := proc(t, p) local tscinde, a, b; if p = 0 then return t else tscinde := Scinder(t, p); a := triFusion(tscinde[0], p - 1); b := triFusion(tscinde[1], p - 1); return Fusionner(a, b, p - 1) end if end proc > print(triFusion(u,3)); array(0 .. 7, [ (0) = 0 (1) = 0 (2) = 4 (3) = 4 (4) = 8 (5) = 9 (6) = 12 (7) = 25 ]) >