Compare commits

...

2 Commits

Author SHA1 Message Date
901ec91940 Prześlij pliki do 'zad2' 2018-06-06 19:00:43 +00:00
972f30d937 Prześlij pliki do '' 2018-05-31 06:20:14 +00:00
4 changed files with 287 additions and 0 deletions

44
Main.java Normal file
View File

@ -0,0 +1,44 @@
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n;
if (args.length < 1) {
n = -1;
Scanner scanner = new Scanner(System.in);
while (n < 2) {
System.out.print("n = ");
n = scanner.nextInt();
}
} else { n = Integer.parseInt(args[0]); }
Ring r = new Ring(n);
r.wyznaczOdwracalne();
r.wyznaczDzielniki();
r.wyznaczNil();
r.wyznaczId();
System.out.println("elementy odwracalne (" + r.odwracalne.size() + "):");
for (int i: r.odwracalne) {
System.out.print(i + ", ");
}
System.out.println();
System.out.println("dzielniki zera (" + r.dzielniki.size() + "):");
for (int i: r.dzielniki) {
System.out.print(i + ", ");
}
System.out.println();
System.out.println("elementy nilpotentne (" + r.nilpotentne.size() + "):");
for (int i: r.nilpotentne) {
System.out.print(i + ", ");
}
System.out.println();
System.out.println("elementy idempotentne (" + r.idempotentne.size() + "):");
for (int i: r.idempotentne) {
System.out.print(i + ", ");
}
}
}

89
Ring.java Normal file
View File

@ -0,0 +1,89 @@
import java.util.ArrayList;
public class Ring {
int n;
ArrayList<Integer> odwracalne;
ArrayList<Integer> dzielniki;
ArrayList<Integer> nilpotentne;
ArrayList<Integer> idempotentne;
public Ring(int n) {
this.n = n;
odwracalne = new ArrayList<>();
dzielniki = new ArrayList<>();
nilpotentne = new ArrayList<>();
idempotentne = new ArrayList<>();
}
private int nwd(int a, int b){
while (a != b){
if (a > b) a -=b;
else b -= a;
}
return a;
}
private int phi(int a){
float result = a;
for (int p = 2; p * p <= a; ++p) {
if (a % p == 0) {
while (a % p == 0)
a /= p;
result *= (1.0 - (1.0 / (float)p));
}
}
if (a > 1)
result *= (1.0 - (1.0 / (float)a));
return (int)result;
}
static int modularPower(int x, int y, int p){
int res = 1;
x = x % p;
while (y > 0){
if((y & 1)==1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void wyznaczOdwracalne(){
for (int i = 1; i < n; ++i){
if (nwd(i, n) == 1) odwracalne.add(i);
}
}
void wyznaczDzielniki(){
for (int i = 1; i < n; ++i){
for (int j = 1; j < n; ++j){
if(((i * j) % n) == 0){
dzielniki.add(i);
break;
}
}
}
}
void wyznaczNil(){
for (int i = 1; i < n; ++i){
for (int j = 1; j <= phi(n); ++j){
if (modularPower(i, j, n) == 0){
nilpotentne.add(i);
break;
}
}
}
}
void wyznaczId(){
int pow;
for (int i = 0; i < n; ++i){
pow = (i * i) % n;
if (pow == i){
idempotentne.add(i);
}
}
}
}

54
zad2/Main.java Normal file
View File

@ -0,0 +1,54 @@
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> f = new ArrayList<>();
ArrayList<Integer> g = new ArrayList<>();
int n;
Scanner scanner = new Scanner(System.in);
Polynomial polynomial;
System.out.print("Podaj n: ");
n = scanner.nextInt();
polynomial = new Polynomial(n);
System.out.print("Najwyższa potęga współczynników f: ");
n = scanner.nextInt();
for (int i = 0; i <= n; ++i){
System.out.print("x^" + i + " = ");
f.add(scanner.nextInt());
}
System.out.print("Najwyższa potęga współczynników g: ");
n = scanner.nextInt();
for (int i = 0; i <= n; ++i){
System.out.print("g^" + i + " = ");
g.add(scanner.nextInt());
}
// polynomial = new Polynomial(2);
// f.add(1);
// f.add(1);
// f.add(1);
// f.add(0);
// f.add(1);
//
// g.add(0);
// g.add(1);
// g.add(1);
ArrayList<Integer> result = polynomial.multiply(f, g);
for (Integer i : result){
System.out.print(i + " ");
}
System.out.println();
result = polynomial.divide(f, g);
for (Integer i : result){
System.out.print(i + " ");
}
// System.out.println();
// result = polynomial.nwd(f, g);
// for (Integer i : result){
// System.out.print(i + " ");
// }
}
}

100
zad2/Polynomial.java Normal file
View File

@ -0,0 +1,100 @@
import java.util.ArrayList;
public class Polynomial {
private int n;
public Polynomial(int n) {
this.n = n;
}
public ArrayList<Integer> normalize(ArrayList<Integer> w){
for (int i = w.size()-1; i >= 0; --i){
if (w.get(i).equals(0)) {
w.remove(i);
}else {
return w;
}
}
return w;
}
public ArrayList<Integer> multiply(ArrayList<Integer> f, ArrayList<Integer> g){
ArrayList<Integer> result = new ArrayList<>();
int tmp;
for (int i = 0; i < f.size(); ++i){
for (int j = 0; j < g.size(); ++j){
tmp = f.get(i) * g.get(j);
if (i + j < result.size()){
tmp += result.get(i+j);
result.set(i + j, tmp % n);
} else {
while ((i + j) >= (result.size())){
result.add(0);
}
result.set(i + j, tmp % n);
}
}
}
return normalize(result);
}
public ArrayList<Integer> multiplyByConst(ArrayList<Integer> f, Integer a){
ArrayList<Integer> result = new ArrayList<>();
for (Integer i : f){
result.add((i * a) % n);
}
return result;
}
public ArrayList<Integer> subtract(ArrayList<Integer> f, ArrayList<Integer> g){
ArrayList<Integer> result = new ArrayList<>();
for (int i = 0; i < f.size(); ++i){
if (i < g.size()){
result.add(Math.floorMod(f.get(i) - g.get(i), n));
} else{
result.add(f.get(i));
}
}
return result;
}
private int getZero(int a, int b){
int i = 0;
while (true){
if ((a + (-(b * i))) == 0){
return i;
}
++i;
}
}
public ArrayList<Integer> divide(ArrayList<Integer> f, ArrayList<Integer> g){
ArrayList<Integer> quotient = new ArrayList<>();
ArrayList<Integer> remainder = new ArrayList<>(f);
ArrayList<Integer> tmpList;
int a = f.size() - 1;
int b = g.size() - 1;
for (int i = 0; i < a - b + 1; ++i){
quotient.add(0);
}
int q = quotient.size()-1;
int tmp;
while (a >= b) {
tmp = getZero(remainder.get(a), g.get(b));
quotient.set(q--, tmp);
tmpList = multiplyByConst(g, tmp);
while(tmpList.size() <= a) tmpList.add(0,0);
remainder = subtract(remainder, tmpList);
--a;
}
return normalize(remainder);
}
public ArrayList<Integer> nwd(ArrayList<Integer> f, ArrayList<Integer> g){
if (g.size() == 0) return f;
return nwd(g, divide(f, g));
}
}