整数分解的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
}
{
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;
{
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];
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;
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 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;
xmax = norm - xmax;
int modm = (int)(m & 0xF);
int modnorm = (int)(norm & 0xf);
int modk = (int)(k & 0xf);
int modp = (int)(p & 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];
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++;
}
}
{
good[length] = j;
length++;
}
}
for (int w = 0; w < (norm - m + 2 * k) / 8; w++)
{
bool finish = false;
{
bool finish = false;
for (int z = 0; z < length; z++)
{
count++;
{
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;
long b = m + j - k * 2;
long sqq = m + j;
sqq = sqq * sqq + 4 * k * j * norm - 4 * k * p;
if (sqq < 0)
{
continue;
}
{
continue;
}
if (!isSquare(sqq))
{
continue;
}
{
continue;
}
long sq2 = (long)Math.Floor(Math.Sqrt(sqq));
long x = (-b + sq2) / 2 / k;
if (x > xmax)
{
finish = true;
break;
}
if (x > xmax)
{
finish = true;
break;
}
if (sqq > sq2 * sq2) // sq2 is not integer
continue;
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;
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";
}
}
break;
}
}
if (found || finish)
break;
}
if (found)
break;
else
label3.Text = "Not Found";
}
}
private bool isSquare(long n)
{
long m;
{
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
// 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;
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;
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;
// 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;
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;
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;
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();
}
{
long number = long.Parse(textBox2.Text);
long p = long.Parse(textBox3.Text);
long q = number * p;
textBox1.Text = q.ToString();
}
阅读全文
0 0
- 整数分解的C#代码
- 快速整数分解C#
- 整数的质数分解
- 整数的分解
- 整数的幂分解
- 整数因子的分解
- 整数分解成多个连续的整数序列
- 输出整数的所有分解
- 整数的质因数分解算法
- 整数的素数分解算法
- 积最大的整数分解
- 整数的素因子分解
- 整数的素因子分解
- 整数的素数和分解
- 阶乘的整数分解 fzu1753
- 积最大的整数分解
- 整数分解的标程
- 整数分解
- 推荐好用的分页插件PageHelper
- 特征值与特征向量在图像处理中的应用
- WebAssembly 实践:如何写代码
- sessionstorage中存储JSON数据
- Directx11教程二十五之ZBuffer(深度缓存,DepthBuffer)
- 整数分解的C#代码
- 二级分销定制技术开发系统
- Listview gridview TV 焦点落在item上面修改背景色
- 不同的路径
- java笔试题及解答专辑201710241157
- 大数据正在推动新零售发展
- phpstorm的几种激活方式
- spring boot实战(番外篇)整合RabbitMQ
- Android Studio 安装插件ADB Idea