Example #1
0
 static public int countDeduction(int a, int k, int m)
 {
     if (trial_div(m))
     {
         if (GCD.countGcd(a, m) == 1)
         {
             Console.WriteLine($"{a}^{k}(mod{m})={a}^{k%(m-1)}(mod{m})={BigInteger.ModPow(a,k%(m-1),m)}");
         }
         else
         {
             Console.WriteLine($"{a} and {m} not simple");    //
         }
     }
     else
     {
         Console.WriteLine("m - composite");
         Dictionary <int, int> mp = new Dictionary <int, int>();
         mp = primeFactorization.count(m); //primeFactorization
         List <int> b  = new List <int>();
         List <int> mi = new List <int>();
         foreach (var item in mp)
         {
             mi.Add(item.Key);
             b.Add((int)Math.Pow(a, k % (item.Key - 1)));
         }
         Console.WriteLine();
         int x = ChinesseAlgorithm.count(b, mi);
         if (x != -1)
         {
             Console.WriteLine($"{a}^{k}(mod{m})= {x}");
         }
     }
     return(0);
 }
Example #2
0
 static bool mutualSimplicity(int a, int b)
 {
     if (GCD.countGcd(a, b) == 1)
     {
         return true;
     }
     else return false;
 }
Example #3
0
        public static void calcCompare(int PrevX, int PrevMod, int ModNumber)
        {
            int GCDCount = GCD.countGcd(PrevX, ModNumber);

            if (PrevMod % GCDCount != 0)
            {
                Console.WriteLine("Error - can't find compare number");
            }
            else
            {
                Console.WriteLine($"Found {GCDCount} solutions");
                if (GCDCount != 1)
                {
                    PrevX     /= GCDCount;
                    PrevMod   /= GCDCount;
                    ModNumber /= GCDCount;
                    Console.WriteLine($"{PrevX}X= {PrevMod} (mod {ModNumber})");
                }
                int ResultNumber = (int)(Math.Pow(PrevX, EulerFunction.calcEulerFunction(ModNumber) - 1)) % ModNumber;
                if (ResultNumber < 0)
                {
                    ResultNumber += ModNumber;
                }
                Console.WriteLine($"ResultNumber of {PrevX} = {ResultNumber}");
                PrevX   = ResultNumber * PrevX;
                PrevMod = ResultNumber * PrevMod;
                Console.WriteLine($"{PrevX}x = {PrevMod}(mod {ModNumber})");
                if (PrevX > ModNumber)
                {
                    PrevX %= ModNumber;
                }
                if (PrevMod > ModNumber)
                {
                    PrevMod %= ModNumber;
                }
                Console.WriteLine($"x = {PrevMod} + {ModNumber}k");
                for (int i = 0; i < GCDCount; i++)
                {
                    Console.WriteLine($"x = {PrevMod+ModNumber*i} (mod {GCDCount*ModNumber})");
                }
            }
        }
        public static int count(List <int> b, List <int> m)
        {
            int  size  = b.Count;
            int  M     = 1;
            bool check = true;

            for (int i = 0; i < size; i++)
            {
                M *= m[i];
                if (i > 0 && i < size - 1)
                {
                    if (GCD.countGcd(m[i - 1], m[i]) != 1)
                    {
                        check = false;
                    }
                }
            }
            if (check)
            {
                int[] Mi = new int[size];
                int[] Yi = new int[size];
                int   x  = 0;
                for (int i = 0; i < size; i++)
                {
                    Mi[i] = M / m[i];
                    Yi[i] = (int)reverseElement.calcReverseElem(Mi[i], m[i]);
                    x    += b[i] * Mi[i] * Yi[i] % M;
                }
                if (x > M)
                {
                    x = x % M;
                }
                Console.WriteLine($"{x} + { M}t");
                return(x);
            }
            else
            {
                Console.WriteLine("No solution");
                return(-1);
            }
        }
Example #5
0
        public static void countRoot(int m)
        {
            int phi = EulerFunction.calcEulerFunction(m);
            Dictionary <int, int> fact = primeFactorization.count(phi);
            int count = 0;

            foreach (var item in fact)
            {
                if (GCD.countGcd(item.Key, m) == 1)
                {
                    bool check = true;
                    foreach (var itemNext in fact)
                    {
                        if (BigInteger.ModPow(item.Key, itemNext.Key, m) == 1)
                        {
                            check = false;
                        }
                    }
                    if (check)
                    {
                        count++;
                        Console.WriteLine();
                        Console.WriteLine($"{item.Key} - Antiderivative root");
                        Console.Write($"U({m})= " + "{");
                        for (var i = 0; i < phi; i++)
                        {
                            Console.Write($" {BigInteger.ModPow(item.Key, i, m)} ");
                        }
                        Console.WriteLine("}");
                    }
                }
            }
            if (count == 0)
            {
                Console.WriteLine(" - Not antiderivative root");
            }
        }
 static public Nullable <int> calcReverseElem(int firstNumber, int secondNumber)
 {
     try
     {
         if (GCD.countGcd(firstNumber, secondNumber) != 1)
         {
             throw new Exception("Error - Not found reverse element");
         }
         else
         {
             int x, y, gcd = decompositionGCD.countDegGCD(firstNumber, secondNumber, out x, out y);
             while (x < 0)
             {
                 x = x + secondNumber;
             }
             return(x);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(null);
     }
 }