zajecia1
This commit is contained in:
commit
1bb7241fd4
2
zajecia1/euklides.py
Normal file
2
zajecia1/euklides.py
Normal file
@ -0,0 +1,2 @@
|
||||
#rozszerzony algorytm euklidesa
|
||||
|
30
zajecia1/potegowanie.py
Normal file
30
zajecia1/potegowanie.py
Normal file
@ -0,0 +1,30 @@
|
||||
#potęgowanie binarne (left-to-right)
|
||||
|
||||
def potegowanie(x, k):
|
||||
y = 1
|
||||
i = k.bit_length()
|
||||
while i >= 0:
|
||||
y = y**2
|
||||
if (k & 1) == 1:
|
||||
y = y*x
|
||||
k = k >> 1
|
||||
i = i - 1
|
||||
return y
|
||||
|
||||
|
||||
def potegowanieimod(x, k, n):
|
||||
y = 1
|
||||
i = k.bit_length()
|
||||
while i >= 0:
|
||||
y = y**2 % n
|
||||
if (k & 1) == 1:
|
||||
y = y*x % n
|
||||
k = k >> 1
|
||||
i = i - 1
|
||||
return y
|
||||
|
||||
|
||||
print(potegowanie(2, 6))
|
||||
|
||||
print(potegowanieimod(5, 2, 10))
|
||||
|
Loading…
Reference in New Issue
Block a user