You are on page 1of 3

Thut ton tnh m v tnh nghch o

- An ton bo mt thng tin -

Thut ton Bnh phng v nhn


Thut ton bnh phng v nhn l thut ton tnh nhanh ly tha t nhin ca mt s
(thc hoc nguyn), trong trng hp c s l s nguyn c th c rt gn theo
mt mun no . c s dng nhiu trong cc bi ton m ha v gii m cc h m
ha, c bit l h m ha cng khai.
x

Tnh a mod n
To bng:
x
in gi tr x

a
in gi tr a

d=1

Khi to d=1

Vi iu kin ch cht: X l th tnh, X chn th b qua.


VD 15

35

mod 79

d=1

35

15

15

17

67

57

65

38

X chn => ko tnh li d


Vn tnh li x, a nh trn
X chn => tng t trn

22

X chn => tng t trn

10

17

X chn => tng t trn

Khi to d=1
X l => tnh li d, x, a trong IF
d := d * (a mod n) = 1 * (15 mod 79) = 15
x := x div 2 = 35 div 2 = 17
a := (a * a) mod n = (15 * 15) mod 79 = 67
X l => tnh li d, x, a tng t trn

X = 0 => dng tnh, ly d cui cng (d = 17) l kt qu

Gi m m phng:
d := 1;
while (x <> 0) do begin
if (x mod 2 <> 0) then begin
d := d * (a mod n);
end;
x := x div 2;
a := (a * a) mod n;
end;
exit(d); {gi tr cn tnh}

Thut ton Euclid m rng


Thut ton Euclid m rng tm phn t nghch o vi m-un cho trc. c s dng nhiu
trong cc bi ton m ha v gii m cc h m ha, c bit l h m ha c in.
Tnh n

-1

mod m

iu kin: n v m phi l 2 s nguyn t cng nhau (ngha l UCLN(n,m)==1)


Nu n==1 th kt qu = 1 (khng cn tnh).

Tng qut:
Gi tr in sn:

x1 = m; a1 = 1; b1 = 0

in gi tr m

Rng

x2 = n; a2 = 0; b2 = 1

in gi tr n

y2

xi

ai

bi

yi

Gi tr tnh ton:
xi = xi-2 mod xi-1

ai = ai-2 (ai-1 * yi-1)

yi = xi-1 div xi

bi = bi-2 (bi-1 * yi-1)

Dng khi xi = 1, kt qu: bi


Nu xi = 0 => Khng tn ti phn t nghch o.
VD1: 35
1
2
3
4
5

x
79
35
9
8
1

VD2: 19
1
2
3
4
5
6

-1

-1

x
26
19
7
5
2
1

mod 79
a
1
0
1
-3

b
0
1
-2
9
-9

y
2
3
1

Kt qu: -9 mod m = -9 mod 79 = 70


Cch tnh chia d vi s m: cng lin
tc vi s chia cho n khi c kt qu
dng.
while (b < 0) do b := b + m;

mod 26
a
1
0

b
0
1
-1
3
-4
11

y
1
2
1
2

Kt qu: 11
Thc t, tm p s ca , ch cn tnh
3 ct x, b, y. Ct a khng cn tnh.

Gi m m phng:
var x, a, b, y: array;
var i: integer;
x[0] := m; x[1] := n;
a[0] := 1; a[1] := 0;
b[0] := 0; b[1] := 1;
if (a = 1) then exit(1)
else if (ucln(n,m) = 1) then exit(null)
else begin
I := 1;
while (x[i] > 1) do begin
y[i] := x[i-1] div x[i];
i := i + 1;
x[i] := x[i-2] div x[i-1];
a[i] := a[i-2] (a[i-1] * y[i-1]);
b[i] := b[i-2] (b[i-1] * y[i-1]);
end;
while (b[i-1] < 0) do b[i-1] := b[i-1] + m;
exit(b[i-1]);
end;

About me
Author:
Gmail:
Facebook:
Demo online:
Source code:

Tng Huynh
tunghuynh.tn@gmail.com
www.facebook.com/tunghuynh.tn
http://tunghuynh.com/atbmtt
http://tunghuynh.com/atbmtt/soft
Moderator at VN-Zoom.com
Studied at ICTU - Thi Nguyn

You might also like

  • Dap An29
    Dap An29
    Document2 pages
    Dap An29
    manhmeotn
    No ratings yet
  • TTTN Mau Dang Ky
    TTTN Mau Dang Ky
    Document2 pages
    TTTN Mau Dang Ky
    manhmeotn
    No ratings yet
  • Chuongtrinhdich 1
    Chuongtrinhdich 1
    Document10 pages
    Chuongtrinhdich 1
    HocLieuMo
    100% (1)
  • Chuongtrinhdich
    Chuongtrinhdich
    Document109 pages
    Chuongtrinhdich
    Co Khong
    No ratings yet
  • Dap An2
    Dap An2
    Document2 pages
    Dap An2
    manhmeotn
    No ratings yet
  • De PTTK
    De PTTK
    Document10 pages
    De PTTK
    manhmeotn
    No ratings yet
  • Dap An22
    Dap An22
    Document4 pages
    Dap An22
    manhmeotn
    No ratings yet
  • Dap An26
    Dap An26
    Document2 pages
    Dap An26
    manhmeotn
    No ratings yet
  • Dap An28
    Dap An28
    Document2 pages
    Dap An28
    manhmeotn
    No ratings yet
  • Dap An24
    Dap An24
    Document2 pages
    Dap An24
    manhmeotn
    No ratings yet
  • Dap An25
    Dap An25
    Document4 pages
    Dap An25
    manhmeotn
    No ratings yet
  • Dap An10
    Dap An10
    Document1 page
    Dap An10
    manhmeotn
    No ratings yet
  • Dap An1
    Dap An1
    Document1 page
    Dap An1
    manhmeotn
    No ratings yet
  • Dap An23
    Dap An23
    Document1 page
    Dap An23
    manhmeotn
    No ratings yet
  • Dap An14
    Dap An14
    Document1 page
    Dap An14
    manhmeotn
    No ratings yet
  • Dap An20
    Dap An20
    Document4 pages
    Dap An20
    manhmeotn
    No ratings yet
  • Dap An21
    Dap An21
    Document1 page
    Dap An21
    manhmeotn
    No ratings yet
  • Dap An16
    Dap An16
    Document2 pages
    Dap An16
    manhmeotn
    No ratings yet
  • Dap An17
    Dap An17
    Document2 pages
    Dap An17
    manhmeotn
    No ratings yet
  • Dap An18
    Dap An18
    Document2 pages
    Dap An18
    manhmeotn
    No ratings yet
  • Dap An13
    Dap An13
    Document1 page
    Dap An13
    manhmeotn
    No ratings yet
  • Dap An12
    Dap An12
    Document1 page
    Dap An12
    manhmeotn
    No ratings yet
  • Dap An11
    Dap An11
    Document1 page
    Dap An11
    manhmeotn
    No ratings yet
  • Dap An19
    Dap An19
    Document3 pages
    Dap An19
    manhmeotn
    No ratings yet
  • Dap An15
    Dap An15
    Document2 pages
    Dap An15
    manhmeotn
    No ratings yet
  • Dap An8
    Dap An8
    Document2 pages
    Dap An8
    manhmeotn
    No ratings yet
  • Dap An9
    Dap An9
    Document1 page
    Dap An9
    manhmeotn
    No ratings yet
  • Dap An5
    Dap An5
    Document3 pages
    Dap An5
    manhmeotn
    No ratings yet
  • Dap An7
    Dap An7
    Document2 pages
    Dap An7
    manhmeotn
    No ratings yet
  • Dap An6
    Dap An6
    Document2 pages
    Dap An6
    manhmeotn
    No ratings yet