Advertisement
Skylighty

zzz

Nov 24th, 2020
3,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import math
  2.  
  3. def euclides2(x,y):
  4.     temp = 0
  5.     coeff = 0
  6.     rest = -1
  7.     while rest != 0:
  8.         if (x > y):
  9.             rest = x % y
  10.             coeff = math.floor(x/y)
  11.             print(str(x) + ' = ' + str(coeff) + '*' + str(y) + ' + ' + str(rest) + ' -> ' +
  12.                   str(rest) + ' = ' + str(x) + ' - (' + str(y) + '*' + str(coeff) + ')')
  13.             x = y
  14.             y = rest
  15.         else:
  16.             rest = y % x
  17.             coeff = math.floor(y/x)
  18.             print(str(y) + ' = ' + str(coeff) + '*' + str(x) + ' + ' + str(rest) + ' -> ' +
  19.                   str(rest) + ' = ' + str(y) + ' - (' + str(x) + '*' + str(coeff) + ')')
  20.             y = x
  21.             x = rest
  22.  
  23.  
  24. # Rozklad na czynniki pierwsze
  25. # Zwraca w liscie, od gory
  26. def distribution(x):
  27.     ite = 2
  28.     dividers = []
  29.     while x > 1:
  30.         if x % ite == 0:
  31.             temp = x
  32.             x /= ite
  33.             dividers.append(ite)
  34.             print(str(temp) + '  |  ' + str(ite))
  35.             ite = 2
  36.         else:
  37.             ite += 1
  38.     return dividers
  39.  
  40.  
  41. # Strukturyzja do postaci slownika klucz-wartosc
  42. # Gdzie klucz to podstawa, a wartosc wykladnik (ilosc wystapien poszczegolnego dzielnika w rozkladzie)
  43. def structurize_divs(divers):
  44.     result = {}
  45.     for i in divers:
  46.         result[i] = divers.count(i)
  47.     return result
  48.  
  49.  
  50. # Funkcja Eulera (nie twierdzenie)
  51. def euler(x):
  52.     arr = distribution(x)
  53.     data = structurize_divs(arr)
  54.     result = 1
  55.     for base in data:
  56.         result *= pow(base, data[base] - 1) * (base - 1)
  57.         print(str(base)+'^('+str(data[base])+'-1)'+'*('+str(base)+'-1)', end=' * ')
  58.     print('\n')
  59.     print('Wynik wynosi : ' + str(result))
  60.  
  61.  
  62. def leftright(x, power, mod):
  63.     # Konwersja na binarke do listy
  64.     bins = [int(i) for i in list('{0:0b}'.format(power))]
  65.     it1 = 0
  66.     it2 = 1
  67.     it3 = len(bins)-1
  68.     z = 0
  69.     z = (pow(pow(x, bins[it1]), 2)) % mod
  70.     print('w' + str(it2) + ' = (' + str(x) + '^(n' + str(it3) + '))^2 mod' + str(mod) + ' = ' + str(z))
  71.     it1 += 1
  72.     it2 += 1
  73.     it3 -= 1
  74.     y = (z*pow(x, bins[it1])) % mod
  75.     print('w' + str(it2) + ' = ' + str(z) + '*' + str(x) + '^(n' + str(it3) + ') mod' + str(mod) + ' = ' + str(y))
  76.     it1 += 1
  77.     it2 += 1
  78.     it3 -= 1
  79.     print('\n')
  80.     while it1 < len(bins):
  81.         z = (pow(y, 2)) % mod
  82.         print('w' + str(it2) + ' = ' + str(y) + '^2 mod' + str(mod) + ' = ' + str(z))
  83.         it2 += 1
  84.         y = (z * pow(x, bins[it1])) % mod
  85.         print('w' + str(it2) + ' = ' + str(z) + '*' + str(x) + '^(n' + str(it3) + ') mod' + str(mod) + ' = ' + str(y))
  86.         print('\n')
  87.         it1 += 1
  88.         it2 += 1
  89.         it3 -= 1
  90.  
  91. def rightleft(x, power, mod):
  92.     # Konwersja na binarke do listy
  93.     bins = [int(i) for i in list('{0:0b}'.format(power))]
  94.     z = 0
  95.     y = 0
  96.     n = len(bins)-1
  97.     i = 1
  98.     ni = 0
  99.     z = x % mod
  100.     print('z'+str(i)+' = '+str(x)+'^(2^0) mod'+str(mod)+' = '+str(z))
  101.     y = (pow(z, bins[n])) % mod
  102.     print('y'+str(i)+' = 1*'+str(x)+'^(n'+str(ni)+') mod'+str(mod)+' = '+str(y)+'\n')
  103.     n -= 1
  104.     i += 1
  105.     ni += 1
  106.     while n >= 0:
  107.         tempz = z
  108.         tempy = y
  109.         z = (pow(z, 2)) % mod
  110.         print('z'+str(i)+' = '+str(tempz)+'^(2) mod'+str(mod)+' = '+str(z))
  111.         y = (y*pow(z, bins[n])) % mod
  112.         print('y' + str(i) + ' = ' + str(tempy) + '*' + str(z) + '^(n' + str(ni) + ') mod' + str(mod) + ' = ' + str(y)+'\n')
  113.         n -= 1
  114.         i += 1
  115.         ni += 1
  116.  
  117.  
  118. def modulcalc(x, mod):
  119.     middle = math.floor(x/mod)
  120.     result = x - (middle*mod)
  121.     print(str(x)+'mod'+str(mod)+' = '+str(x)+' - ['+str(x)+'/'+str(mod)+']*'+str(mod)+' = '+str(result))
  122.  
  123. # Aby uzyc wystarczy wpisac odpowiednia funkcje i skompilowac program, funkcje dostepne opisane sa ponizej
  124.  
  125. # Przyklad uzycia : na dole dopisuje modulcalc(135,5), daje Run - otrzymuje wynik, jak w matlabie.
  126.  
  127. # Funkcje do uzycia:
  128. # Modulo (z ujemnymi tez) - modulcalc(x,mod), gdzie x to liczba, mod liczba modulo pokazuje zapis
  129. # NWD - euclides2(x,y) gdzie x,y to liczby dla ktorych liczone jest NWD
  130. # Funkcja Eulera - euler(x) gdzie x to liczba z ktorej jest funkcja, ostatnia gwiazdke w zapisie poteg ignoruj
  131. # Metoda z lewa na prawo - lefright(x, power, mod) gdzie x to liczba, power wykladnik, mod to wartosc modulo
  132. # Metoda z prawa na lewo - rightleft(x, power, mod) - tak samo jak z lewa na prawo
  133.  
  134. rightleft(3,64,11)
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement