(*DE 1*)

Code:

Clear[L];
n = Input["nhap n"];
L = Table[Random[Integer, {-100, 100}], {i, n}];
Print["danh sach vua tao :", L];

(*cau a : in ra pt dau, giua, cuoi*)
Print["phan tu dau:", First[L]];
Print["phan tu giua :", Part[L[[Ceiling[n/2]]]]];
Print["phan tu cuoi :", Last[L]];
(*cau b*)
Print["phan tu nho nhat:", Min[L]];

(*cau c*)
Tao[L_] := Module[{L1, n, i},
         L1 = {};
      n = Length[L];
      For[i = 1, i ≤ n, i++,
        If[PrimeQ[L[[i]]],
               L1 = Append[L1, L[[i]]]];
        ];
      Return [L1];
      ];
Print["ds nguyen to vua tao:", Tao[L]];
(*cau e*)
K = Select[L, PrimeQ[#] &];
Print[K];
Print["tong:", Sum[K[[i]], {i, Length[K]}]];

(*cau f*)

BubbleSort[L_] := Module[{L1, i, j, n, temp},
      L1 = L;
      n = Length[L1];
      For[i = 1, i ≤ n, i++,
           For[j = n, j ≥ i, j--,
               If[L[[i]] < L[[j]],
                temp = L1[[i]];
                L1[[i]] = L1[[j]];
                L1[[j]] = temp;
                ];
            ];
        ];
      Return [L1];
     
      ];
Print["danh sach vua sap xep L=:", BubbleSort[L]];

(*cau g : them phan tu bat ky sao cho ds van giam*)

a = Input["nhap so can them :"];
b = Input["nhap vi tri can them:"];
K = Insert[L, a, b];
Print["ds vua them :", BubbleSort[K]];

(*cau f : xoa bat ky*)
m = Input["nhap vi trican xoa :"];
Print["ds vua bi xoa :", Delete[L, m]];

(*cau i : tim kiem nhi phan*)

BinarySearch[L_, x_, i_, j_] := Module[{Mid},
      Mid = Ceiling[(i*j)/2];
         If[L[[Mid]] == x,
              Return  [True]];,
         If[L[[Mid]] > x,
        Return[BinarySearch[BubbleSort[L], x, i, Mid - 1]];
        Return[BinarySearch[BubbleSort[L], x, Mid + 1, j]];
        ];
     
      Return  [False];
      ];
Print["so can tim :", BinarySearch[L, 1, 4, -2]];

(*cau j1 : liet ke phan tu xuat hien nhieu lan nhat*)
Print["danh sach L:", Sort[L]];
temp = Union[L];
temp1 = Table[Count[L, temp[[i]]], {i, Length[temp]}];
temp2 = {};
m = Max[temp1];
For[i = 1, i ≤ Length[temp1], i++,
       If[temp1[[i]] == m,
           temp2 = Append[temp2, temp[[i]]]];
    ];
Print[temp];
Print[temp1];
Print["cac phan tu xuat hien nhieu lan nhat:", temp2];

(*cau j2 : cac phan tu xuat hien it nhat*)
Print["danh sach L dc sap xep:", Sort[L]];
L1 = Union[L];
L2 = {};

L3 = Table[Count[L, L1[[i]]], {i, Length[L1]}];
m = Min[L3];
For[i = 1, i ≤ Length[L3], i++,
       If[L3[[i]] == m,
           L2 = Append[L2, L1[[i]]];
        ];
    ];
Print["pt xuat hien it nhat:", L2];

(*CAU 2*)
n = Input["nhap n:"];

A = Table[Random[Integer, {-30, 30}], {i, n}, {j, n}];
Print["A =", MatrixForm[A]];

(*cau a*)
Print["dinh thuc ma tran :", Det[A]];
(*cau b chuyen vi*)
Print["matran chuyen vi :", MatrixForm[Transpose[A]]];

(*cau c : tao ma tran dg cheo 9*)
n = Length[A];
B = Table[If[i == j, A[[i, j]], 0], {i, n}, {j, n}];
Print["ma tran duong cheo chinh :", MatrixForm[B]];

(*cau d : tong*)
Print["tong cac phan tu trong mt A:", Plus @@ Plus @@ A];
Print["tong cac phan tu trong mt A:", Sum[A[[i, j]], {i, n}, {j, n}]];

(*cau e*)
temp1 = {};
temp = 0
For[i = 1, i ≤ Length[A], i++,
       For[j = 1, j ≤ Length[A], j++,
              If[PrimeQ[A[[i, j]]],
               temp = temp + A[[i, j]];
            temp1 = Append[temp1, A[[i, j]]];
            ];
        ];
    ];
Print["tong NT:", temp];
Print["ds NT :", temp1];

Print["tong nguyen to =", Sum[If[PrimeQ[A[[i, j]]], A[[i, j]],
            0], {i, n}, {j, n}]];

(*cau f : them dong *)
Clear[i, temp];
m = n;
  i = 0;
While[i < 1 || i > n,
       i = Input["nhap dong can them >0 va <=" <> ToString[n]];
    ];
temp = Table[Random[Integer, {-10, 10}], {j, m}]
A = Insert[A, temp, i];
n++;
Print["A=", MatrixForm[A]];
 

  (*g : xoa 1 dong khoi ma tran A*)
Clear[i];
i = 0;
While[i < 1 || i > n, i = Input["nhap dong can xoa >0 va <=" <> ToString[n]];
    ];
A = Delete[A, i];
n--;
Print["A=", MatrixForm[A]];

(*cau h : liet ke cac phan tu xuat hien tren cac dong*)
Print["Pt xuat hien tren moi dong:", Intersection[Delete[A, 0]]];

(*cau i : dem phan tu khac nhau*)
Print["so phan tu mang gt khac nhau:", Count[Union[Delete[A, 0]], _Integer]];

(*cau j : sap xep ma tran tang theo tung cot *)
A = Transpose[A];

A = Table[Sort[A[[i]]], {i, Length[A]}];;
A = Transpose[A];
Print["Sap xep A=", MatrixForm[A]];

(*CAU 3*)
F[n_] := If[n == 1 || n == 2, Return  [1],
         
         Return [F[n - 1] + F[n - 2]]];


n = Input["nhap n"];
Print["F(n)=", F[n]];
(*cau 3/b*)
n = Input["nhap n"];
A = Table[i, {i, n}];
Print[A];
S[A_, n_] := If[n == 1, Return  [A[[1]]],
         Return[A[[n]]S[A, n - 1]];
      ];

Print["tich n phan tu:", S[A, n]];