/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.vutbr.feec.utko.fojtoval.krypto.funkce;

/**
 * Vypocty pomoci Euklidova algoritmu.
 * @author fojtoval
 */
public class Euclides extends Statistika{


    /**
     * Nejvetsi spolecny delitel cisel x (parametr1) a y (parametr2).
     * @param param1
     * @param param2
     * @return vysledek
     */
    public static int gcd(int param1, int param2) {

        int tmp = 0;

        while (param2 != 0) {
            tmp = param1;
            param1 = param2;
            param2 = tmp % param2;
        }

        return(param1);
    }

    /**
     * Vypocet multiplikativni inverze cisla x v mnozine y.
     * @param x
     * @param y
     * @return vysledek
     */
    public static int euc(int x, int y) {

        int[] u = {1, 0, x};
        int[] v = {0, 1, y};
        int[] t = new int[3];

        while (v[2] != 0) {
            int q = u[2]/v[2];
            for (int i = 0; i < 3; i++) {
                t[i] = u[i] - v[i]*q;
                u[i] = v[i];
                v[i] = t[i];
            }
        }

        if (u[1]<0) {
            return (u[1]+x);
        } else  {
              return u[1];
          }

    }

}
