You are on page 1of 24

MC LC

Ni dung
Trang
Chng 1 THUT TON ........................................................................................5
I. NGUYN L CHUNG ...........................................................................6
1) t vn ..................................................................................................6
2) Biu din s nguyn ln ............................................................................6
II. GII THUT THC HIN PHP TON S NGUYN LN ......7
1) Php cng ..................................................................................................7
ALGORITHM 1 CNG CC S NGUYN .................................7
2) Php tr .....................................................................................................8
ALGORITHM 2 TR CC S NGUYN ...................................8
3) Php nhn ..................................................................................................8
ALGORITHM 3 NHN CC S NGUYN .................................8
4) Php chia ...................................................................................................9
ALGORITHM 4 CHIA CA HAI S NGUYN ..........................9
5) So snh hai s ...........................................................................................10
6) c chung ln nht (UCLN) ....................................................................11
ALGORITHM 6 THUT TON EUCLID ...................................11
7) Ly Tha ...................................................................................................11
ALGORITHM 7 LY THA xb ....................................................11
ALGORITHM 7.1 XY DNG KHAI TRIN C S b..............12
8) Nghch o.................................................................................................12
ALGORITHM 8 Nghch o c s b theo modulo m......................12
Chng 2 K THUT LP TRNH .......................................................................13
I. CHNG TRNH BNG JAVA ..........................................................14
1) Phn tch .......................................................................................14
2) Thit k giao din ..........................................................................14
3) Thit k module .............................................................................14
4) Thit k code ................................................................................15
5) Chy th v kim tra .....................................................................20
II. CHNG TRNH BNG PASCAL ...................................................20
Chng 3 CHY TH.............................................................................................25
TI LIU THAM KHO ........................................................................................26

http://www.ebook.edu.vn

chng

1 thut ton

Ni dung chng
Trnh by cc thut ton thc hin cc php ton Cng, Tr, Nhn,
DIV, MOD, Ly tha, UCLN, Nghch o ca cc s a v b trn vnh
Zn.
Ti liu tham kho chnh ca phn ny l [1], [2], [3] v [4].

http://www.ebook.edu.vn

I. NGUYN L CHUNG
1) t vn
Cc my PC thng thng l my 32 bit, v th kiu d liu nguyn thng dng nm
trong on [-231 (-2,147,483,648), 231 1 (2,147,483,647)]. Nh vy chng ta c th
thc hin php tnh s hc vi s nguyn ln n hng t. Ta cng c th s dng c bit
du tng ln s nguyn c biu din.
Nhiu ngn ng lp trnh cung cp kiu d liu nguyn ln hn, chng hn 64 bit
hoc 128 bit biu din s nguyn. V d 64 bit th c th biu din cc s nguyn nm
trong [-263 (- 9,223,372,036,854,775,808), 263-1 (9,223,372,036,854,775,807)].
Vic s dng s thc vi chnh xc kp c th lm vic vi cc s ln hn rt
nhiu. Tuy nhin, khng th s dng chng vo mc ch m cc s nguyn ln v kt
qu l khng chnh xc.
thc hin tnh ton cc s nguyn ngoi phm vi biu din c cung cp, ta cn
t thit k ly cch biu din v cc hm thc hin cc php ton c bn vi cc s
nguyn ln.
2) Biu din s nguyn ln
Thng thng ta s dng hai cch biu din s nguyn ln nh sau:
Mng cc ch s: Mt cch n gin nht biu din cc s nguyn ln l s dng
mng cc ch s ca n, trong phn t u tin ca mng l ch s c ngha hng cao
nht ca s nguyn cn biu din. Rt c th ta s s dng thm mt bin ghi nhn s ch
s c th thc hin cc php ton nhanh hn.
Danh sch lin kt cc ch s: Cu trc ng l cn thit khi ta phi lm vic vi cc
s nguyn c ln ty (tc l khng bit trc hn ch v s ch s). Lu rng cc
s nguyn c 100 000 ch s l kh ln v ta c th ct tr mt s nh vy bng mt
bng 100 000 byte.
Vic s dng b nh ng tht s hu ch khi mun tn dng mt cch hiu qu b
nh. Nu ch cn lm vic vi cc s nguyn vi s ch s b chn th nn dng mng cc
ch s. Sau y l mt cch biu din s ln c m phng:
Const MAXDIGITS =100 (* di cc i ca s *)
PLUS =1 (* bit du khng m *)
MINUS = -1 (* bit du m *)
Type
BigNum = Record
digits: array [0 .. MAXDIGTS] of Byte; (* Cc ch s *)
signbit: integer (* du PLUS hay MINUS *)
lastdigit: integer; (* Ch s ca ch s c ngha cao nht *)
End;

Hnh 1-1: Mt cch khai bo cch biu din s ln phng trnh .


T nguyn l chung ny, ta c th thc hin cc php ton v lp trnh n da vo cc
gii thut s c trnh by phn II. y cng cn lu rng, ng vi mi ngn ng
lp trnh, chng ta c th c cc cng c thc hin vic cc php ton mt cch nhanh
chng hn (bng cch s dnghttp://www.ebook.edu.vn
cc th vin m cc nh lp trnh chuyn nghip dn

sn, chng ta ch vic s dng!) thay v phi lp trnh tng chi tit ca thut ton li t
u va mt thi gian li va khng mang li hiu qu (xt v mt thc thi).
II. CC GII THUT THC HIN PHP TON S NGUYN LN
1) Php cng
Thc hin t phi qua tri v phn nh c mang sang tri. Nu mt trong hai s l
m th n t ng chuyn sang php tr theo nguyn tc ph thng.
ALGORITHM 1 CNG CC S NGUYN
Input: a, b
Output Cho kt qu tng ca a v b
Method:
Procedure Cng (a, b positive integers)
{Khai trin nh phn ca a v b tng ng l
(an-1...a1a0)2 v (bn-1...b1b0)2 (xem thm ALGORITHM 7.1)
}
c:=0
for j:=0 to n-1
Begin
d := (aj+bj+c)/2
sj := aj+bj+c-2d
c :=d
End
Sn :=c
{Khai trin nh phn ca tng l (Sn-1...S1S0)2}

Hnh 1-2: Thut ton m phng cng hai s nguyn (ly t [4] trang 177)
M phng c th:
Procedure Add_bigNum (a,b: BigNum; var c:BigNum); (* Thc hin php cng c = a +b *)
var
carry: integer (* nh *)
i: integer;
begin
initialize_bignum(c)
if (a.signbit = b.signbit) then c.signbit = a.signbit
else
begin
if (a.signbit = MINUS ) then
begin
a.signbit := PLUS;
subtract_bignum(b,a,c);
a.signbit :=MINUS;
end
else
begin
b.signbit := PLUS;
subtract_bignum(a,b,c);
b.signbit :=MINUS;
end;
exit;
end;
c.lastdigit := max (a.lastdigit, b.lastdigit) +1;
carry :=0;
for i:=0 to c.lastdigit do
begin
c.digits[i] := (carry + a.digits[i] + b.digits[i]) mod 10;
carry := (carry + a.digits[i] + b.digits[i]) div 10;
end
zero_justify (c);
end;

Hnh 1-3: Thc hin thuthttp://www.ebook.edu.vn


ton cng hai s nguyn c = a + b.

Ch :
Th tc zero_justify tnh lastdigit loi b cc ch s 0 khng c ngha u:
procedure zero_justify (var n: bignum);
begin
while ((n.lastdigits>0) and (n.digits [n.lastdigit] =0)) do dec (n.lastdigits);
if ((n.lastdigit =0) and (n.digits[0] = 0)) then n.signbit = PLUS; (trnh TH -0)
end;

Hnh 1-4: Th tc loi b ch s 0 khng c ngha u s n.


2) Php tr
Php tr ngc li vi php nh php cng, ta phi ch vay mng hng cao
hn. n gin ta nen iu chnh sao cho s b tr l s ln hn.
ALGORITHM 2 TR CC S NGUYN
{Tng t thut ton cng}
M phng :
procedure subtract_bignum (a,b:bignum; var c: bignum);
var
borrow: integer;
v, i: integer;
begin
if ((a.signbit = MINUS ) or (b.signbit = MINUS )) then
begin
b.signbit := -1 *b.signbit;
add_bignum(a,b,c);
b.signbit := -1 *b.signbit;
exit;
end;
if compare_bignum(a,b)=1 then
begin
subtract_bignum (b,a,c);
c.signbit :=MINUS;
exit;
end;
c.lastdigit := max (a.lastdigit, b.lastdigit) ;
borrow :=0;
for i:=0 to c.lastdigit do
begin
v := (a.digits [i] -borrow - b.digits[i];
if (a.digits[i]>0) then borrow :=0;
if v <0 then
begin
v:=v+10;
borrow:=1;
end;
c.digits[i]:= v mod 10;
end;
zero_justify (c);
end;

ln m phng bng PASCAL, trong th tc ny thc


Hnh 1-5: Tr cc s nguyn
http://www.ebook.edu.vn
hin php tr c = a - b.

3) Php nhn
Thc hin thut ton nhn ph thng
ALGORITHM 3 NHN CC S NGUYN
Input: a, b
Output Cho kt qu tch ca a v b
Method:
Procedure NHAN (a, b : positive Integers)
{Khai trin nh phn ca a v b tng ng l
(an-1...a1a0)2 v (bn-1...b1b0)2 (xem thm ALGORITHM 7.1)
}
for j :=1 to n -1
begin
if bj =1 then cj :=a c dch i j ch
else cj :=0
end
{c0, c1, ..., cn-1 l cc tch ring tng phn}
p:=0
for j:=1 to n-1
p:= p+cj
{p l gi tr tch a, b}

Hnh 1-6: Thut ton nhn cc s nguyn (ly t [4] trang 179).
M phng:
procedure multiply_bignum(a,b:bignum; var c:bignum);
var
row: bignum; (*dong dich trai *)
tmp: bignum;
i,j: integer;
begin
initialize_bignum (c);
row:=a;
for i:=0 to b.lastdigit do
begin
for j:=1 to b.digits[i] do
begin
add_bignum(c,row,tmp);
c:=tmp;
end;
digit_shift(row,1);
end;
c.signbit:=a.signbit +b.signbit;
zero_justify (c);
end;

Hnh 1-7: M phng thut ton nhn c = a * b.


Mi bc nhn i hi dch chuyn s th nht sang tri mt v tr v cng n d ln
vo kt qu, vi d l ch s tng ng ca s th hai.
procedure digit_shift( var n:bignum; d:integer);
(* nhan n vi 10^d *)
var
i:integer;
begin
if ((n.lastdigit =0) and (n.digits[0]=0)) then exit;
for i:=n.lastdigit downto 0 do n:digit[i+d]:=n.digits[i];
for i:=0 to d-1 do n.digits[i]:=0;
n.lastdigit :=n.lastdigit+d;
end;

http://www.ebook.edu.vn
Hnh 1-8: Thut ton dch chuyn sang tri mt v tr v cng dn.

4) Php chia
Bn cht ca php chia cng ging nh php nhn, do vy xy dng gii thut chia
cng gn ging vi gii thut nhn, chi tit c m t nh sau
ALGORITHM 4 CHIA CA HAI S NGUYN
{Tng t ALGORITHM 3}
M phng:
procedure divide_bignum(a,b:bignum; var c:bignum);
var
row: bignum; (*dong dich trai *)
tmp: bignum;
asign, bsign: integer;
i,j: integer;
begin
initialize_bignum (c);
c.signbit:=a.signbit *b.signbit;
asign := a.signbit;
bsign := b.signbit;
b.signbit := PLUS;
a.signbit := PLUS;
initialize_bignum (row);
initialize_bignum (tmp);
c.lastdigit :=a.lastdigit;
for i:=a.lastdigit downto 0 do
begin
digit_shift(row,1);
row.digits[0]:=a.digits[i];
c.digits[i]:=0;
while (compare_bignum (row, b) <> PLUS) do
begin
inc(c.digits[i]);
subtract_bignum(row, b, tmp);
row:=tmp;
end;
end;
zero_justify (c);
a.signbit :=asign ;
b.signbit :=bsign ;
end;

Hnh 1-9: Th tc thc hin php chia c = a:b


Ch : Th tc ny thc hin php chia nguyn m khng tr li phn d ca php
chia a cho b. Nu mun tm phn d ca a:b c th thc hin tnh a b (a div b) nh th
tc ci t.
5) So snh hai s ALGORITHM 5
Gii thut: Vic so snh hai s c thc hin t hng ch cao nht, bt u t bit
u. Chi tit c th m t nh sau:
M phng:
function compare_bignum (a,b:bignum):integer;
var i: integer;
begin
if ((a.signbit = MINUS ) and (b.signbit = PLUS )) then
begin
compare_bignum :=1;
exit;
end;
if ((a.signbit = PLUS ) and (b.signbit = MINUS )) then
begin
compare_bignum :=-1;
http://www.ebook.edu.vn
exit;

end;
if (b.lastdigit > a.lastdigit) then
begin
compare_bignum := PLUS * a.signbit;
exit;
end;
if (a.lastdigit > b.lastdigit) then
begin
compare_bignum := MINUS * a.signbit;
exit;
end;
for i:=a.lastdigit downto 0 do
begin
if (a.digits[i] > b.digits[i]) then
begin
compare_bignum := MINUS * a.signbit;
exit;
end;
if (b.digits[i] > a.digits[i]) then
begin
compare_bignum := PLUS * a.signbit;
exit;
end;
end;
end;

Hnh 1-10: So snh hai s nguyen ln a v b.


6) c chung ln nht (UCLN)
ALGORITHM 6. THUT TON EUCLID
Input: a, b
Output: c chung ln nht ca a v b
METHOD:
Procedure UCLN(a,b: Positive integers)
x:=a;
y:=b;
while y <>0
begin
r:= x mod y
x:=y
y:=r
end {UCLN (a,b) l x}

Hnh 1-11: Thut ton -c-lt tm UCLN ca a v b.


7) Ly Tha
Gi s c cc s x, b v n. Ta phi tm xb mod n. Vi cc s x, a, n kh ln th php
tnh kh cng knh mt nhiu thi gian. Trong trng hp ny ta dng thut ton Bnh
phng v nhn nh sau:
Biu din s m b di dng nh phn ta c mt s nh phn di k:
k

b = bi 2 k i
i =1

trong bi =0 hoc 1. Khi


k

x b = x bi 2 k i nu bi=0 th
i =1

x bi 2

k i

= 1 , cn nu bi=1 th x b 2

http://www.ebook.edu.vn
Do , ta c thut ton xb mod n nh sau:

k i

= x2

k i 1

ALGORITHM 7 LY THA xb
Input: x v b
Output: xb
Method:
1. Begin
2. Mu =1
3. For i:=1 to k do mu:=mu*mu mod n
4. If bi=1 then mu:=mu*x mod n
5. End

Hnh 1-12: Thut ton tnh x ly tha b


ALGORITHM 7.1 XY DNG KHAI TRIN C S b
INPut: n, b
Output: Khai trin c s b ca n
Method:
Procedure khai tin c s b ( n: positive integers)
q:=n;
k:=0;
while q <>0
begin
ak :=q mod b;
q:= cn di nguynln nht ca (q/p)
k:=k+1;
end {Khai trin c b ca n l (ak-1 ...a1a0)b}

Hnh 1-13: Thut ton khai trin n thnh mt s c c s l b.


8) Nghch o
ALGORITHM 8 NGHCH O CA S b THEO MODULO m
INput: b, m
Output: b-1 (Nu b kh nghch)
METHOD:
1. Begin
r0 :=m; r1:= b; q=r0 div r1; r2:=r0 mod r1; t0:=0; t1:=1;
2. While r2>0 do
Begin
t2 :=to t1*q;
r0 :=r1; r1:=r2;
q:=r0 div r1;
r2:=r0 mod r1;
t0 :=t1;
t1:=t2
End
3. If r1=1 then Output b-1 =t2
Else Output b khng kh nghch
4. End.

Hnh 1-14: Thut ton tm nghch o c s b theo modulo n

http://www.ebook.edu.vn

chng 2

k thut lp trnh

Ni dung chng
Trong chng ny, chng em gii thiu code th hin cc thut
ton chng 1 bng JAVA v PASCAL, c bit y chng
em gii thiu mt SIU CHNG TRNH bng ngn ng
JAva (theo li ca cc nh lp trnh hng SUN
MICROSYSTEMS) c th qun l mt s c hng ngn ch s.
Cc ni dung trnh by chnh:

o Phn tch
o Thit k giao din
o Thit k module
o Thit k code
o Chy th v kim tra

http://www.ebook.edu.vn

I. CHNG TRNH BNG JAVA


1) Phn tch
Theo yu cu bi ton, thc hin cc gii thut chng 1 trn mt giao din thn
thin ngi dng, v vy chng em chn ngn ng lp trnh Java vi kiu chng trnh
Applet c th chy trn web.
Nu khng xt v k thut lp trnh, chng em c th khng nh chng trnh ca
mnh l ngn gn v chy nhanh nht trong s cc chng trnh cng loi. C s
khng nh iu ny l:
i) Chnh hng SUN khng nh nh vy, xem [5]
ii) Chng em test bng cch cho chy cc chng trnh cng loi nhng lp trnh bng
ngn ng khc hoc theo phng php khc.

2) Thit k giao din


Giao din chnh ca chng trnh c thit k nh sau:

Cc v tr 1 l v tr label dng hng dn ngi dng nhp cc s vo.


V tr 2 l cc TextBox dng cha cc s m ngi dng nhp vo ng thi xut
kt qu ra.
V tr 3 l cc Button dng thc hin cc thao tc m ngi dng yu cu.

3) Thit k module
Sau y l cc module v ngha ca n:

public class TinhSoLon extends Applet implements ActionListener, KeyListener


{

public static void main(String[] args)


http://www.ebook.edu.vn

public void start()


public void stop()
public BigInteger Cong(BigInteger Zn,BigInteger A, BigInteger B)
public BigInteger Tru(BigInteger Zn,BigInteger A, BigInteger B)
public BigInteger Nhan(BigInteger Zn,BigInteger A, BigInteger B)
public BigInteger Div(BigInteger Zn,BigInteger A, BigInteger B)
public BigInteger Mod(BigInteger Zn,BigInteger A, BigInteger B)
public BigInteger UCLN(BigInteger Zn,BigInteger A, BigInteger B)
public void NghichDao(BigInteger Zn,BigInteger A)
public BigInteger LuyThua(BigInteger Zn,BigInteger A, BigInteger B)
public void TrangThai()
public void actionPerformed(ActionEvent e)
public void keyPressed(KeyEvent e)
public void keyReleased(KeyEvent e)
public void keyTyped(KeyEvent e)
public void TinhSoLon()
public void KhoiTao()
}

4) Thit k code
import java.security.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;
import javax.swing.*;
public class TinhSoLon extends Applet implements ActionListener,
KeyListener
{
public JPanel PN1,PN2,PN3,PN4,PN5,PN6;
TextArea tZn,tA,tB,tKQ,tTT;
JLabel lZn,lSA,lSB,lKQ;
JButton Cong,Tru,Nhan,Div,Mod,UCLN,NgD,LuyThua;
public static void main(String[] args)
{
TinhSoLon applet = new TinhSoLon ();
JFrame frame = new JFrame();
frame.setTitle("Tinh so lon Zn");
frame.add(applet,BorderLayout.CENTER);
frame.setSize(600, 380);
//applet.start();
http://www.ebook.edu.vn
applet.KhoiTao();

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation( (d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.show();
frame.setVisible( true );
}
public void start()
{
KhoiTao();
}
public void stop()
{
}
public BigInteger Cong(BigInteger Zn,BigInteger A, BigInteger B)
{
return (A.add(B).mod(Zn));
}
public BigInteger Tru(BigInteger Zn,BigInteger A, BigInteger B)
{
BigInteger temp;
temp=A.subtract(B);
if (temp.compareTo(new BigInteger("0"))<0)
{
temp=Zn.add(temp);
}
return (temp);
}
public BigInteger Nhan(BigInteger Zn,BigInteger A, BigInteger B)
{
return (A.multiply(B).mod(Zn));
}
public BigInteger Div(BigInteger Zn,BigInteger A, BigInteger B)
{
BigInteger t1,t2,t3;
t1=A.mod(B);
t2=A.subtract(t1);
t3=t2.divide(B);
return (A.divide(B));
}
public BigInteger Mod(BigInteger Zn,BigInteger A, BigInteger B)
{
return (A.mod(B));
}
public BigInteger UCLN(BigInteger Zn,BigInteger A, BigInteger B)
{
return (A.gcd(B));
}
public void NghichDao(BigInteger Zn,BigInteger A)
{
if (Zn.gcd(A).compareTo(new BigInteger("1"))==0)
{
tKQ.setText(A.modInverse(Zn).toString());
}
else
{
http://www.ebook.edu.vn
tKQ.setText("So A khong co nghich dang tren truong Zn");

}
}
public BigInteger LuyThua(BigInteger Zn,BigInteger A, BigInteger B)
{
return (A.modPow(B,Zn));
}
public void TrangThai()
{
String st="Ghi chu:Zn co ";
st=st+ String.valueOf(tZn.getText().trim().length())+" chu so, so A
co ";
st=st+ String.valueOf(tA.getText().trim().length())+" chu so, so B
co ";
st=st+ String.valueOf(tB.getText().trim().length())+" chu so, ket
qua co ";
st=st+ String.valueOf(tKQ.getText().trim().length())+" chu so";
tTT.setText(st);
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
tZn.setText(tZn.getText().trim());
tA.setText(tA.getText().trim());
tB.setText(tB.getText().trim());
BigInteger Zn,A,B;
//Zn=BigInteger.probablePrime(100,1);
try
{
Zn=new BigInteger(tZn.getText());
A=new BigInteger(tA.getText());
B=new BigInteger(tB.getText());
int comp=100000;
comp=Zn.compareTo(A);
if
((Zn.compareTo(A)>=0)&(Zn.compareTo(B)>=0)&(Zn.compareTo(new
BigInteger("0"))>=0)&
(A.compareTo(new
BigInteger("0"))>=0)&(B.compareTo(new BigInteger("0"))>=0))
{
if (cmd=="Cong")
{
tKQ.setText(Cong(Zn,A,B).toString());
}
else if (cmd=="Tru")
{
tKQ.setText(Tru(Zn,A,B).toString());
}
else if (cmd=="Nhan")
{
tKQ.setText(Nhan(Zn,A,B).toString());
}
else if (cmd=="Div")
{
tKQ.setText(Div(Zn,A,B).toString());
}
http://www.ebook.edu.vn

else if (cmd=="Mod")

{
tKQ.setText(Mod(Zn,A,B).toString());
}
else if (cmd=="USCLN")
{
tKQ.setText(UCLN(Zn,A,B).toString());
}
else if
(cmd=="Luy thua")
{
tKQ.setText(LuyThua(Zn,A,B).toString());
}
}
else
{
tKQ.setText("Gia tri A, B phai nho hon Zn va cac so phai khong am");
}
}
catch (Exception err)
{
tKQ.setText("Kiem tra lai so vanh Zn, A va B");
}
///// Tinh nghich dao thi kiem tra dieu kien rieng
try
{
Zn=new BigInteger(tZn.getText());
A=new BigInteger(tA.getText());
if ((Zn.compareTo(A)>=0)&(Zn.compareTo(new
BigInteger("0"))>=0)& (A.compareTo(new BigInteger("0"))>=0))
{
if (cmd=="Nghich dao A")
{
tKQ.setText("");
NghichDao(Zn,A);
}
}
else
{
tKQ.setText("Gia tri A phai nho hon Zn va cac so phai khong
am");
}
}
catch (Exception err)
{
tKQ.setText("Kiem tra lai so vanh Zn, A");
}
TrangThai();
}
public void keyPressed(KeyEvent e)
{
}
http://www.ebook.edu.vn
public void keyReleased(KeyEvent e)

{
TrangThai();
}
public void keyTyped(KeyEvent e)
{
}
public void TinhSoLon()
{
}
public void KhoiTao()
{
//setLayout(new BorderLayout());
PN1=new JPanel();
PN2=new JPanel();
PN3=new JPanel();
PN4=new JPanel();
PN5=new JPanel();
PN6=new JPanel();
this.add(PN1);
this.add(PN2);
this.add(PN3);
this.add(PN4);
this.add(PN5);
this.add(PN6);
///////////////////////////
lZn=new JLabel("Vanh Zn:");
lSA=new JLabel("So A:
");
lSB=new JLabel("So B:
");
lKQ=new JLabel("Ket qua: ");
PN1.add(lZn);
PN2.add(lSA);
PN3.add(lSB);
PN4.add(lKQ);
/////////////////////////////
tZn=new TextArea("100",2,70,1);
//tZn.setText()
tA=new TextArea("1",2,70,1);
tB=new TextArea("1",2,70,1);
tKQ=new TextArea("",2,70,1);
tTT=new TextArea("Ghi chu",1,80,3);
tZn.addKeyListener(this);
tA.addKeyListener(this);
tB.addKeyListener(this);
tTT.setEnabled(false);
tTT.setBackground(Color.red);
PN1.add(tZn);
PN2.add(tA);
PN3.add(tB);
PN4.add(tKQ);
PN6.add(tTT);
Cong=new JButton("Cong");
Tru=new JButton("Tru");
Nhan=new JButton("Nhan");
Div=new JButton("Div");
Mod=new JButton("Mod");
UCLN=new JButton("USCLN");
http://www.ebook.edu.vn
NgD=new JButton("Nghich dao A");

LuyThua=new JButton("Luy thua");


Cong.addActionListener(this);
Tru.addActionListener(this);
Nhan.addActionListener(this);
Div.addActionListener(this);
Mod.addActionListener(this);
UCLN.addActionListener(this);
NgD.addActionListener(this);
LuyThua.addActionListener(this);
PN5.add(Cong);
PN5.add(Tru);
PN5.add(Nhan);
PN5.add(Div);
PN5.add(Mod);
PN5.add(UCLN);
PN5.add(LuyThua);
PN5.add(NgD);
TrangThai();
}
}

5) Chy th v kim tra


Chng em chy th vi u vo l hng ngn ch s [xem chng 3]

II. CHNG TRNH BNG PASCAL


program BT;
uses crt;
Const MAXDIGITS =100 (* di ti a *);
PLUS =1 (* bit du khng m *);
MINUS = -1 (* bit du m *);
Type
BigNum = Record
digits: array [0 .. MAXDIGITS] of Byte; (* Cc ch s *)
signbit: integer (* du PLUS hay MINUS *);
lastdigit: integer; (* Ch s ca ch s c ngha cao nht *)
End;
var a, b, c: bignum;
f:file of bignum;
i:integer;
(*------------------------------------------------------------*)
procedure subtract_bignum (a,b:bignum; var c: bignum); forward;
Function max (a,b:integer):integer;
begin
if a>= b then max:=a else max:=b;
end;
(*------------------------------------------------------------*)
procedure initialize_bignum (var c:bignum);
var i:integer;
begin
c.signbit:=PLUS;
c.lastdigit:=0;
http://www.ebook.edu.vn
for i:=0 to MAXDIGITS
do c.digits[i]:=0;

end;
(*------------------------------------------------------------*)
procedure printNum (a:bigNum);
var
i, j: integer;
begin
if (a.signbit = -1) then write ('-');
j:=a.lastdigit;
for i:=j downto 0 do
begin
write (a.digits[i]);
end;
end;
(*------------------------------------------------------------*)
procedure Nhapso (var a: bignum);
var s:string;
i, j, t, codee: integer;
begin
initialize_bignum (a);
write ('dau cua so:'); readln (a.signbit);
write ('Nhap day chu so: '); readln (s);
a.lastdigit := length (s) -1; j:=a.lastdigit;
for i:=j+1 downto 1 do
begin
val (s[i], t, codee);
a.digits[j-i+1]:=t;
end;
end;
(*------------------------------------------------------------*)
procedure zero_justify (var n: bignum);
begin
while ((n.lastdigit>0) and (n.digits [n.lastdigit] =0)) do dec
(n.lastdigit);
if ((n.lastdigit =0) and (n.digits[0] = 0)) then n.signbit := PLUS; (*trnh
TH -0*)
end;
(*------------------------------------------------------------*)
function compare_bignum (a,b:bignum):integer;
var i: integer;
begin
if ((a.signbit = MINUS ) and (b.signbit = PLUS )) then
begin
compare_bignum :=1;
exit;
end;
if ((a.signbit = PLUS ) and (b.signbit = MINUS )) then
begin
compare_bignum :=-1;
exit;
end;
if (b.lastdigit > a.lastdigit) then
begin
http://www.ebook.edu.vn
compare_bignum := PLUS * a.signbit;

exit;
end;
if (a.lastdigit > b.lastdigit) then
begin
compare_bignum := MINUS * a.signbit;
exit;
end;
for i:=a.lastdigit downto 0 do
begin
if (a.digits[i] > b.digits[i]) then
begin
compare_bignum := MINUS * a.signbit;
exit;
end;
if (b.digits[i] > a.digits[i]) then
begin
compare_bignum := PLUS * a.signbit;
exit;
end;
end;
end;
(*------------------------------------------------------------*)
Procedure Add_bigNum (a,b: BigNum; var c:BigNum);
(* Th?c hi?n php c?ng c = a +b *)
var
carry: integer (* nh? *);
i: integer;
begin
initialize_bignum(c);
if (a.signbit = b.signbit) then c.signbit := a.signbit
else
begin
if (a.signbit = MINUS ) then
begin
a.signbit := PLUS;
subtract_bignum(b,a,c);
a.signbit :=MINUS;
end
else
begin
b.signbit := PLUS;
subtract_bignum(a,b,c);
b.signbit :=MINUS;
end;
exit;
end;
c.lastdigit := max (a.lastdigit, b.lastdigit) +1;
carry :=0;
for i:=0 to c.lastdigit do
begin
c.digits[i] := (carry + a.digits[i] + b.digits[i])mod 10;
carry := (carry + a.digits[i] + b.digits[i]) div 10;
end;
zero_justify (c);
end;
(*------------------------------------------------------------*)
http://www.ebook.edu.vn
procedure subtract_bignum (a,b:bignum; var c: bignum);

var
borrow:integer;
v:integer;
i:integer;
begin
if ((a.signbit = MINUS ) or (b.signbit = MINUS )) then
begin
b.signbit := -1 *b.signbit;
add_bignum(a,b,c);
b.signbit := -1 *b.signbit;
exit;
end;
if compare_bignum(a,b)=1 then
begin
subtract_bignum (b,a,c);
c.signbit :=MINUS;
exit;
end;
c.lastdigit := max (a.lastdigit, b.lastdigit) ;
borrow :=0;
for i:=0 to c.lastdigit do
begin
v := (a.digits [i] -borrow - b.digits[i]);
if (a.digits[i]>0) then borrow :=0;
if v <0 then
begin
v:=v+10;
borrow:=1;
end;
c.digits[i]:= v mod 10;
end;
zero_justify (c);
end;
(*------------------------------------------------------------*)
procedure digit_shift (var n:bignum; d:integer);
(* nhan n voi 10^d *)
var
i:integer;
begin
if ((n.lastdigit =0) and (n.digits[0]=0)) then exit;
for i:=n.lastdigit downto 0 do n.digits[i+d]:=n.digits[i];
for i:=0 to d-1 do n.digits[i]:=0;
n.lastdigit :=n.lastdigit+d;
end;
(*------------------------------------------------------------*)
procedure multiply_bignum(a,b:bignum; var c:bignum);
var
row: bignum; (*dong dich trai *)
tmp: bignum;
i,j: integer;
begin
initialize_bignum (c);
row:=a;
for i:=0 to b.lastdigit do
begin
for j:=1 to b.digits[i]
do
http://www.ebook.edu.vn
begin

add_bignum(c,row,tmp);
c:=tmp;
end;
digit_shift(row,1);
end;
c.signbit:=a.signbit +b.signbit;
zero_justify (c);
end;
(*------------------------------------------------------------*)
procedure divide_bignum(a,b:bignum; var c:bignum);
var
row: bignum; (*dong dich trai *)
tmp: bignum;
asign, bsign: integer;
i,j: integer;
begin
initialize_bignum (c);
c.signbit:=a.signbit *b.signbit;
asign := a.signbit;
bsign := b.signbit;
b.signbit := PLUS;
a.signbit := PLUS;
initialize_bignum (row);
initialize_bignum (tmp);
c.lastdigit :=a.lastdigit;
for i:=a.lastdigit downto 0 do
begin
digit_shift(row,1);
row.digits[0]:=a.digits[i];
c.digits[i]:=0;
while (compare_bignum (row, b) <> PLUS) do
begin
inc(c.digits[i]);
subtract_bignum(row, b, tmp);
row:=tmp;
end;
end;
zero_justify (c);
a.signbit :=asign ;
b.signbit :=bsign ;
end;
(*------------------------------------------------------------*)
BEGIN
writeln ('Nhap so thu nhat:'); Nhapso (a);
writeln ('Nhap so thu hai:'); Nhapso(b);
writeln;writeln (' Ket qua:');
writeln ('--------------------------------------------:');
writeln ('So thu nhat:'); PrintNum (a); writeln;
writeln ('So thu hai:'); PrintNum (b); writeln;
if compare_bignum(a,b)=1 then writeln (' So thu hai lon hon')
else writeln (' So thu nhat lon hon'); writeln;
(*-----Hieu cua c=a-b--------------*)
subtract_bignum(a,b,c);
writeln ('Hieu cua hai so la '); PrintNum(c); writeln;
(*-----Tong cua c=a-b--------------*)
add_bignum (a,b,c);
http://www.ebook.edu.vn
writeln ('Tong cua hai so la '); PrintNum(c); writeln;

(*-----Nhan cua c=a-b--------------*)


multiply_bignum (a,b,c);
writeln ('Tich cua hai so la '); PrintNum(c); writeln;
(*-----Chia cua c=a-b--------------*)
divide_bignum (a,b,c);
writeln ('Chia cua hai so la '); PrintNum(c); writeln;
readln;
END.

http://www.ebook.edu.vn

chng

3 chy th

Ni dung
Kt qu chy th trnh by a CD km theo ti liu ny

http://www.ebook.edu.vn

TI LIU THAM KHO


A. Ti liu tham kho cho cc thut ton
[1] W. Patterson, Mathematical Cryptology (ti liu photocopy do thy Nguyn
Xun My cung cp).
[2] W.Patterson Mathematical Cryptography for Computer Scientists and
Mathematicians.
[3] Seberry,.J; Pipepzyk,.J, Cryptography: An Introduction to Cryptography
Security.
[4] Kenneth H. Rosen, Ton ri rc ng dng trong tin hc (bn ting vit), NXB
KHKT, 2003

B. Ti liu tham kho cho lp trnh


[5] The on-line Java Documentation, ti
http://java.sun.com/j2se/1.5.0/docs/index.html , http://java.sun.com/docs v
http://developer.java.sun.com
[6] Help ca Turbo Pascal hng Borland

http://www.ebook.edu.vn

You might also like