You are on page 1of 2

CHƯƠNG TRÌNH CON

1. Số nguyên tố
Cách 1:
function snt(a:longint):boolean;
var i:longint;
begin
for i:=2 to trunc(sqrt(a)) do
if (a mod i=0) then exit(false);
exit(a>=2);
end;
Cách 2:
function snt(a:longint):boolean;
var i,dem:longint;
begin
snt:=true;
dem:=0;
for i:=2 to a do
if (a mod i=0) then inc(dem);
if (dem=2) then snt:=false;
end;
Cách 3:
function snt(a:longint):boolean;
var i:longint;
begin
snt:=true;
for i:=2 to trunc(sqrt(a)) do
if (a mod i=0) then snt:=false;
if (a=1) or (a=0) then snt:=true;
end;
2. Số chính phương
function scp(a:longint):boolean;
begin
scp:=false;
if sqr(trunc(sqrt(a)))=a then scp:=true;
end;
3. Ước chung lớn nhất
function ucln(a:longint):longint;
begin
if a=b then ucln:=a else
while a<>b do
if a>b then a:=a-b else b:=b-a;
ucln:=a;
end;
4. Đếm số chữ số
function scs(a:longint):longint;
var s:string;
begin
str(a,s);
scs:=length(s);
end;
5. Số đảo
function sodao(a:longint):longint;
var s,s2:string;
i,k,code:longint;
begin
str(a,s);
s2:=’’;
for i:=length(s) downto 1 do
s2:=s2+s[i];
val(s2,k,code);
sodao:=k;
end;
6. Tổng số chữ số
function tscs(a:longint):longint;
var ch:char;
i,t:longint;
s:string;
begin
t:=0;
str(a,s);
for ch:='1' to '9' do
while (pos(ch,s)>0) do
begin
t:=t+(ord(ch)-48);
delete(s,pos(ch,s),1);
end;
tscs:=t;
end;

You might also like