整数分解的C#代码

来源:互联网 发布:vs2015怎么写c语言 编辑:程序博客网 时间:2024/05/29 18:38
public Form1()
        {
            InitializeComponent();
            textBox1.Text = "56944483"; // 3683 14741
            textBox2.Text = "44449";
            textBox3.Text = "99991";   // find solution with 2446 try
        }
        private void button1_Click(object sender, EventArgs e)
        {
            long number = long.Parse(textBox1.Text);
            long norm = (long)Math.Sqrt(number);
            long norm2 = norm * norm;
            int count = 0;
            int[] modj2 = new int[8] { 0, 1, 4, 9, 0, 9, 4, 1 }; // mod 16
            int[] mod = new int[8];
            int[] good = new int[8];
            label1.Text = "";
            label2.Text = "";
            for (int i = 1; i < 10; i++)  // only search to sqrt(number)/3; or two factors are within 10 times of each other
            {
                bool found = false;
                norm = (long)Math.Sqrt(number / i / 4);
                norm = norm * 2;  // base is even number
                norm2 = norm * norm;
                long k = number / norm2;
                long m = (number - k * norm2) / norm;
                long p = number - k * norm2 - m * norm;
                long xmax = (long)Math.Sqrt(number / (i + 1));  // next base
                xmax = norm - xmax;
                int modm = (int)(m & 0xF);  
                int modnorm = (int)(norm & 0xf);
                int modk = (int)(k & 0xf);
                int modp = (int)(p & 0xf);
                int length = 0;
                for (int j = 0; j < 8; j++)
                {
                    mod[j] = modj2[j] + 2 * modm * j + 4 * modk * j * modnorm + modm * modm - 4 * modk * modp;
                    mod[j] = mod[j] & 0xf;
                    int t = mod[j];
                    if (t == 0 || t == 1 || t == 4 || t == 9)
                    {
                        good[length] = j;
                        length++;
                    }
                }
                for (int w = 0; w < (norm - m + 2 * k) / 8; w++)
                {
                    bool finish = false;
                    for (int z = 0; z < length; z++)
                    {
                        count++;
                        int j = w * 8 + good[z];
                        long b = m + j - k * 2;                       
                        long sqq = m + j;
                        sqq = sqq * sqq + 4 * k * j * norm - 4 * k * p;
                        if (sqq < 0)
                        {
                            continue;
                        }
                        if (!isSquare(sqq))
                        {
                            continue;
                        }
                        long sq2 = (long)Math.Floor(Math.Sqrt(sqq));
                        long x = (-b + sq2) / 2 / k;
                        if (x > xmax)
                        {
                            finish = true;
                            break;
                        }                                            
                        if (sqq > sq2 * sq2)  // sq2 is not integer
                            continue;
                        long spp = 2 * k * x - k + m + j;
                        if (spp < norm - x)
                        {
                            found = true;
                            label1.Text = " " + (norm - x + 1);
                            long nn = (long)(norm - x + 1);
                            long q = number / nn;
                            label2.Text = " " + q;
                            label3.Text = " try count:" + count;
                            break;
                        }
                    }
                    if (found || finish)
                        break;                   
                }
                if (found)
                    break;
                else
                    label3.Text = "Not Found";
            }
        }
        private bool isSquare(long n)
        {
            long m;
            // start with mod 128 rejection. 82% rejection rate
            // VERY fast, can read bits directly
            // http://mersenneforum.org/showpost.php?p=110896
            //       m = n & 0x127; // n mod 128
            //      if (((m * 0x8bc40d7d) & (m * 0xa1e2f5d1) & 0x14020a) != 0) return false; something wrong

            //Other modulii share one BigInt modulus.
            long largeMod = n % (63L * 25 * 11 * 17 * 19 * 23 * 31); // SLOW, bigint modulus
            // residues mod 63. 75% rejection
            m = largeMod % 63; // fast, all 32-bit math
            if (((m * 0x3d491df7) & (m * 0xc824a9f9) & 0x10f14008) != 0) return false;
            // residues mod 25. 56% rejection
            m = largeMod % 25;
            if (((m * 0x1929fc1b) & (m * 0x4c9ea3b2) & 0x51001005) != 0) return false;
            // residues mod 31. 48.4% rejection
            //  Bloom filter has a little different form to keep it perfect
            m = 0xd10d829a * (largeMod % 31);
            if ((m & (m + 0x672a5354) & 0x21025115) != 0) return false;

            // residues mod 23. 47.8% rejection
            m = largeMod % 23;
            if (((m * 0x7bd28629) & (m * 0xe7180889) & 0xf8300) != 0) return false;
            // residues mod 19. 47.3% rejection
            m = largeMod % 19;
            if (((m * 0x1b8bead3) & (m * 0x4d75a124) & 0x4280082b) != 0) return false;
            // residues mod 17. 47.1% rejection
            m = largeMod % 17;
            if (((m * 0x6736f323) & (m * 0x9b1d499) & 0xc0000300) != 0) return false;
            // residues mod 11. 45.5% rejection
            m = largeMod % 11;
            if (((m * 0xabf1a3a7) & (m * 0x2612bf93) & 0x45854000) != 0) return false;
            return true;
        }       
        private void button2_Click(object sender, EventArgs e)
        {
            long number = long.Parse(textBox2.Text);
            long p = long.Parse(textBox3.Text);
            long q = number * p;
            textBox1.Text = q.ToString();
        }
原创粉丝点击