forked from kalmar/DALGLI0
zad 2.
This commit is contained in:
parent
add5ee3825
commit
1d1b6f026a
68
zad2.py
Normal file
68
zad2.py
Normal file
@ -0,0 +1,68 @@
|
||||
def modulo(x,n):
|
||||
for i in range(len(x)):
|
||||
x[i] = x[i] % n
|
||||
return x
|
||||
|
||||
def iloczyn(f,g,n):
|
||||
wynik = [0]*(len(f)+len(g)-1)
|
||||
for i in range(len(f)):
|
||||
for j in range(len(g)):
|
||||
wynik[i+j] += f[i]*g[j]
|
||||
return modulo(wynik,n)
|
||||
|
||||
def liczmod(a,x,n):
|
||||
for i in range(n):
|
||||
if( (a*i) % n == x):
|
||||
return(i)
|
||||
return(-1)
|
||||
|
||||
def sumujmod(a,b,n):
|
||||
if(len(a) < len(b)):
|
||||
wynik = b
|
||||
for i in range(len(a)):
|
||||
wynik[i] += a[i]
|
||||
else:
|
||||
wynik = a
|
||||
for i in range(len(b)):
|
||||
wynik[i] += b[i]
|
||||
return modulo(wynik,n)
|
||||
|
||||
def redukcjazer(x):
|
||||
while x and x[-1] is 0:
|
||||
x.pop()
|
||||
return(x)
|
||||
|
||||
def klasaReszt(f,g,n):
|
||||
wynik = f
|
||||
mnoznik = [0] * (len(f)-len(g)+2)
|
||||
for i in range(len(f)):
|
||||
if(liczmod(g[len(g)-1],wynik[len(f)-1-i],n) == -1):
|
||||
return("DivisionError")
|
||||
if(i > len(f)-len(g)):
|
||||
return redukcjazer(modulo(wynik,n))
|
||||
else:
|
||||
mnoznik.pop()
|
||||
mnoznik.pop()
|
||||
mnoznik.append(-1*liczmod(g[len(g)-1],wynik[len(f)-1-i],n))
|
||||
wynik = sumujmod(wynik, iloczyn(g, mnoznik ,n) ,n)
|
||||
|
||||
def nwd(a,b,n):
|
||||
while(b != []):
|
||||
if(klasaReszt(a,b,n)):
|
||||
return "DivisionError"
|
||||
c = klasaReszt(a,b,n)
|
||||
a = b
|
||||
b = c
|
||||
return modulo(a,n)
|
||||
|
||||
def main():
|
||||
n = int(input("Podaj n: "))
|
||||
print("R[x] dla R = Z/"+str(n))
|
||||
f = eval(input("Podaj f w formacie listy np: [1,1, 0,1]: "))
|
||||
g = eval(input("Podaj f w formacie listy np: [1, 1,0,1]: "))
|
||||
print(iloczyn(f,g,n))
|
||||
print(klasaReszt(f,g,n))
|
||||
print(nwd(f,g,n))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user