Zadanie 2 #39

Open
s422967 wants to merge 4 commits from s422967/DALGLI0:master into master
2 changed files with 205 additions and 0 deletions

106
main.cpp Normal file
View File

@ -0,0 +1,106 @@
#include <iostream>
#include <math.h>
using namespace std;
bool odwracalny(int i, int n){
for(int x=0; x<n; x++){
if((x * i) % n == 1){
return true ;
}
return false;
}
}
void el_odwracalne(int n){
int lista[n];
int index=0;
for(int i=0; i<n; i++){
if(odwracalny(i,n)){
lista[index]=i;
index++;
}
}
for (int i=0; i<index; i++){
cout << lista[i] << endl;
}
}
bool dzieli_zero(int i, int n){
for(int x=0; x<n; x++){
if((x * i) % n == 0){
return true ;
}
return false;
}
}
void dzielniki_zera(int n){
int lista[n];
int index=0;
for(int i=0; i<n; i++){
if(dzieli_zero(i,n)){
lista[index]=i;
index++;
}
}
for (int i=0; i<index; i++){
cout << lista[i] << endl;
}
}
bool nilpotent(int i, int n){
int power=0;
for(int x=0; x<n; x++){
power = pow(x, i);
if(power % n == 0){
return true;
}
return false;
}
}
void el_nilpotentne(int n){
int lista[n];
int index=0;
for(int i=0; i<n; i++){
if(nilpotent(i,n)){
lista[index]=i;
index++;
}
}
for (int i=0; i<index; i++){
cout << lista[i] << endl;
}
}
void el_idempotentne(int n){
int lista[n];
int index=0;
for(int i=0; i<n; i++){
if(i*i % n == i){
lista[index]=i;
index++;
}
}
for (int i=0; i<index; i++){
cout << lista[i] << endl;
}
}
int main()
{
int n = 5;
cout << "(";
el_odwracalne(n);
cout << ")";
dzielniki_zera(n);
el_nilpotentne(n);
el_idempotentne(n);
}

99
zad2.py Normal file
View File

@ -0,0 +1,99 @@
import math
class Polynomial:
n = 0
def __init__(self, n):
self.n = n
def normalize(self, w):
while 0 in w:
w.remove(0)
return w
def multiply(self, f, g):
result = []
tmp=0
for i in range(0, len(f)):
for j in range(0, len(g)):
tmp=f[i]*g[j]
if i+j < len(result):
tmp = tmp + result[i+j]
result[i+j] = tmp % self.n
else:
while i+j >= len(result):
result.append(0)
result[i+j] = tmp % self.n
return self.normalize(result)
def multiplyConst(self, f, a):
result = []
for i in f:
result.append((i*a)%self.n)
return result
def substract(self, f, g):
result = []
for i in range(0, len(f)):
if i < len(g):
result.append(math.fmod(f[i]-g[i], self.n))
else:
result.append(f[i])
return result
def zero(self, a, b):
i = 0
while True:
if (a + (-(b*i))) == 0:
return i
i = i + 1
def divide(self, f, g):
quotient = []
reminder = f
tmpList = []
a = (len(f) - 1)
b = (len(g) - 1)
for i in range(0, a-b+1):
quotient.append(0)
q = len(quotient)-1
tmp = 0
while a>=b:
tmp = self.zero(reminder[a], g[b])
quotient[q] = tmp
q = q - 1
tmpList = self.multiplyConst(g, tmp)
while len(tmpList) <= a:
tmpList.append(0)
reminder = self.substract(reminder, tmpList)
a = a - 1
return self.normalize(reminder)
def nwd(self, f, g):
if len(g)==0:
return f
return self.nwd(g, self.divide(f, g))
polynomial = Polynomial(2)
f = [1, 1, 1, 0, 1]
g = [0, 1, 1]
print(polynomial.multiply(f, g))
print(polynomial.divide(f, g))
print(polynomial.nwd(f, g))
polynomial1 = Polynomial(6)
ff = [2, 1, 0, 2, 1, 3]
gg = [1, 0, 0, 5]
print(polynomial1.multiply(ff, gg))
print(polynomial1.divide(ff, gg))
print(polynomial1.nwd(ff, gg))