예제 #1
0
        private static void LehmerGcd(ref BigIntegerBuilder reg1, ref BigIntegerBuilder reg2)
        {
            var num = 1;

            while (true)
            {
                var num1 = reg1._iuLast + 1;
                var num2 = reg2._iuLast + 1;
                if (num1 < num2)
                {
                    NumericsHelpers.Swap(ref reg1, ref reg2);
                    NumericsHelpers.Swap(ref num1, ref num2);
                }
                if (num2 == 1)
                {
                    if (num1 == 1)
                    {
                        reg1._uSmall = NumericsHelpers.GCD(reg1._uSmall, reg2._uSmall);
                    }
                    else if (reg2._uSmall != 0)
                    {
                        reg1.Set(NumericsHelpers.GCD(Mod(ref reg1, reg2._uSmall), reg2._uSmall));
                    }
                    return;
                }
                if (num1 == 2)
                {
                    break;
                }
                if (num2 > num1 - 2)
                {
                    var high2  = reg1.GetHigh2(num1);
                    var high21 = reg2.GetHigh2(num1);
                    var num3   = NumericsHelpers.CbitHighZero(high2 | high21);
                    if (num3 > 0)
                    {
                        high2  = high2 << (num3 & 63) | reg1._bits[num1 - 3] >> (32 - num3 & 31);
                        high21 = high21 << (num3 & 63) | reg2._bits[num1 - 3] >> (32 - num3 & 31);
                    }
                    if (high2 < high21)
                    {
                        NumericsHelpers.Swap(ref high2, ref high21);
                        NumericsHelpers.Swap(ref reg1, ref reg2);
                    }
                    if (high2 == ulong.MaxValue || high21 == ulong.MaxValue)
                    {
                        high2  = high2 >> 1;
                        high21 = high21 >> 1;
                    }
                    if (high2 == high21)
                    {
                        reg1.Sub(ref num, ref reg2);
                    }
                    else if (NumericsHelpers.GetHi(high21) != 0)
                    {
                        uint num4 = 1;
                        uint num5 = 0;
                        uint num6 = 0;
                        uint num7 = 1;
                        while (true)
                        {
                            uint num8 = 1;
                            var  num9 = high2 - high21;
                            while (num9 >= high21 && num8 < 32)
                            {
                                num9 = num9 - high21;
                                num8++;
                            }
                            if (num9 >= high21)
                            {
                                var num10 = high2 / high21;
                                if (num10 <= uint.MaxValue)
                                {
                                    num8 = (uint)num10;
                                    num9 = high2 - num8 * high21;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            var num11 = num4 + num8 * (ulong)num6;
                            var num12 = num5 + num8 * (ulong)num7;
                            if (num11 > 2147483647 || num12 > 2147483647)
                            {
                                break;
                            }
                            else if (num9 < num12 || num9 + num11 > high21 - num6)
                            {
                                break;
                            }
                            else
                            {
                                num4  = (uint)num11;
                                num5  = (uint)num12;
                                high2 = num9;
                                if (high2 > num5)
                                {
                                    num8 = 1;
                                    num9 = high21 - high2;
                                    while (num9 >= high2 && num8 < 32)
                                    {
                                        num9 = num9 - high2;
                                        num8++;
                                    }
                                    if (num9 >= high2)
                                    {
                                        var num13 = high21 / high2;
                                        if (num13 <= uint.MaxValue)
                                        {
                                            num8 = (uint)num13;
                                            num9 = high21 - num8 * high2;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    num11 = num7 + num8 * (ulong)num5;
                                    num12 = num6 + num8 * (ulong)num4;
                                    if (num11 > 2147483647 || num12 > 2147483647)
                                    {
                                        break;
                                    }
                                    if (num9 < num12 || num9 + num11 > high2 - num5)
                                    {
                                        break;
                                    }
                                    num7   = (uint)num11;
                                    num6   = (uint)num12;
                                    high21 = num9;
                                    if (high21 <= num6)
                                    {
                                        break;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        if (num5 != 0)
                        {
                            reg1.SetSizeKeep(num2, 0);
                            reg2.SetSizeKeep(num2, 0);
                            var num14 = 0;
                            var num15 = 0;
                            for (var i = 0; i < num2; i++)
                            {
                                var num16 = reg1._bits[i];
                                var num17 = reg2._bits[i];
                                var num18 = (long)num16 * num4 - (long)num17 * num5 + num14;
                                var num19 = (long)num17 * num7 - (long)num16 * num6 + num15;
                                num14         = (int)(num18 >> 32);
                                num15         = (int)(num19 >> 32);
                                reg1._bits[i] = (uint)num18;
                                reg2._bits[i] = (uint)num19;
                            }
                            reg1.Trim();
                            reg2.Trim();
                        }
                        else if (high2 / 2 < high21)
                        {
                            reg1.Sub(ref num, ref reg2);
                        }
                        else
                        {
                            reg1.Mod(ref reg2);
                        }
                    }
                    else
                    {
                        reg1.Mod(ref reg2);
                    }
                }
                else
                {
                    reg1.Mod(ref reg2);
                }
            }
            reg1.Set(NumericsHelpers.GCD(reg1.GetHigh2(2), reg2.GetHigh2(2)));
        }
예제 #2
0
 private static void LehmerGcd(ref BigIntegerBuilder reg1, ref BigIntegerBuilder reg2)
 {
     var num = 1;
     while (true)
     {
         var num1 = reg1._iuLast + 1;
         var num2 = reg2._iuLast + 1;
         if (num1 < num2)
         {
             NumericsHelpers.Swap(ref reg1, ref reg2);
             NumericsHelpers.Swap(ref num1, ref num2);
         }
         if (num2 == 1)
         {
             if (num1 == 1)
             {
                 reg1._uSmall = NumericsHelpers.GCD(reg1._uSmall, reg2._uSmall);
             }
             else if (reg2._uSmall != 0)
             {
                 reg1.Set(NumericsHelpers.GCD(Mod(ref reg1, reg2._uSmall), reg2._uSmall));
             }
             return;
         }
         if (num1 == 2)
         {
             break;
         }
         if (num2 > num1 - 2)
         {
             var high2 = reg1.GetHigh2(num1);
             var high21 = reg2.GetHigh2(num1);
             var num3 = NumericsHelpers.CbitHighZero(high2 | high21);
             if (num3 > 0)
             {
                 high2 = high2 << (num3 & 63) | reg1._bits[num1 - 3] >> (32 - num3 & 31);
                 high21 = high21 << (num3 & 63) | reg2._bits[num1 - 3] >> (32 - num3 & 31);
             }
             if (high2 < high21)
             {
                 NumericsHelpers.Swap(ref high2, ref high21);
                 NumericsHelpers.Swap(ref reg1, ref reg2);
             }
             if (high2 == ulong.MaxValue || high21 == ulong.MaxValue)
             {
                 high2 = high2 >> 1;
                 high21 = high21 >> 1;
             }
             if (high2 == high21)
             {
                 reg1.Sub(ref num, ref reg2);
             }
             else if (NumericsHelpers.GetHi(high21) != 0)
             {
                 uint num4 = 1;
                 uint num5 = 0;
                 uint num6 = 0;
                 uint num7 = 1;
                 while (true)
                 {
                     uint num8 = 1;
                     var num9 = high2 - high21;
                     while (num9 >= high21 && num8 < 32)
                     {
                         num9 = num9 - high21;
                         num8++;
                     }
                     if (num9 >= high21)
                     {
                         var num10 = high2 / high21;
                         if (num10 <= uint.MaxValue)
                         {
                             num8 = (uint)num10;
                             num9 = high2 - num8 * high21;
                         }
                         else
                         {
                             break;
                         }
                     }
                     var num11 = num4 + num8 * (ulong)num6;
                     var num12 = num5 + num8 * (ulong)num7;
                     if (num11 > 2147483647 || num12 > 2147483647)
                     {
                         break;
                     }
                     else if (num9 < num12 || num9 + num11 > high21 - num6)
                     {
                         break;
                     }
                     else
                     {
                         num4 = (uint)num11;
                         num5 = (uint)num12;
                         high2 = num9;
                         if (high2 > num5)
                         {
                             num8 = 1;
                             num9 = high21 - high2;
                             while (num9 >= high2 && num8 < 32)
                             {
                                 num9 = num9 - high2;
                                 num8++;
                             }
                             if (num9 >= high2)
                             {
                                 var num13 = high21 / high2;
                                 if (num13 <= uint.MaxValue)
                                 {
                                     num8 = (uint)num13;
                                     num9 = high21 - num8 * high2;
                                 }
                                 else
                                 {
                                     break;
                                 }
                             }
                             num11 = num7 + num8 * (ulong)num5;
                             num12 = num6 + num8 * (ulong)num4;
                             if (num11 > 2147483647 || num12 > 2147483647)
                             {
                                 break;
                             }
                             if (num9 < num12 || num9 + num11 > high2 - num5)
                             {
                                 break;
                             }
                             num7 = (uint)num11;
                             num6 = (uint)num12;
                             high21 = num9;
                             if (high21 <= num6)
                             {
                                 break;
                             }
                         }
                         else
                         {
                             break;
                         }
                     }
                 }
                 if (num5 != 0)
                 {
                     reg1.SetSizeKeep(num2, 0);
                     reg2.SetSizeKeep(num2, 0);
                     var num14 = 0;
                     var num15 = 0;
                     for (var i = 0; i < num2; i++)
                     {
                         var num16 = reg1._bits[i];
                         var num17 = reg2._bits[i];
                         var num18 = (long)num16 * num4 - (long)num17 * num5 + num14;
                         var num19 = (long)num17 * num7 - (long)num16 * num6 + num15;
                         num14 = (int)(num18 >> 32);
                         num15 = (int)(num19 >> 32);
                         reg1._bits[i] = (uint)num18;
                         reg2._bits[i] = (uint)num19;
                     }
                     reg1.Trim();
                     reg2.Trim();
                 }
                 else if (high2 / 2 < high21)
                 {
                     reg1.Sub(ref num, ref reg2);
                 }
                 else
                 {
                     reg1.Mod(ref reg2);
                 }
             }
             else
             {
                 reg1.Mod(ref reg2);
             }
         }
         else
         {
             reg1.Mod(ref reg2);
         }
     }
     reg1.Set(NumericsHelpers.GCD(reg1.GetHigh2(2), reg2.GetHigh2(2)));
 }