var sp = new Stopwatch();
sp.Start();
Parallel.For(1, 10, a =>
{
Parallel.For(1, 10, b =>
{
Parallel.For(1, 10, c =>
{
Parallel.For(1, 10, d =>
{
long left = 25870 + a;
int right = (int)Math.Pow(2, b) * c * d * d * 11; // 2^b * c * d² * 11
if (left == right)
{
Console.WriteLine($"a={a}, b={b}, c={c}, d={d}, sum={left}");
}
});
});
});
});
Console.WriteLine(sp.Elapsed);
//a=2, b=3, c=6, d=7, sum=25872
//a=2, b=4, c=3, d=7, sum=25872
//00:00:00.0154183
//簡單判斷是不是質數,反正在10以內,不可能太久
static bool IsPrime(int n)
{
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
ststic void Calc(){
//計時器,非必要
var sp = new Stopwatch();
sp.Start();
Parallel.For(1, 10, a =>
{
Parallel.For(1, 10, b =>
{
Parallel.For(1, 10, c =>
{
Parallel.For(1, 10, d =>
{
// 檢查 c 是否是質數
if (!IsPrime(c))
return;
int left = 25870 + a;
int right = (int)Math.Pow(2, b) * c * d * d * 11; // 2^b * c * d² * 11
if (left == right)
{
lock (Console.Out)
Console.WriteLine($"a={a}, b={b}, c={c}, d={d}, sum={left}");
}
});
});
});
});
Console.WriteLine(sp.Elapsed);
//a=2, b=4, c=3, d=7, sum=25872
//00:00:00.0189032
}
很無聊就當練習一下,不過無聊的人好像不只有我一個,哈哈哈~~
--
The bug existed in all possible states.
Until I ran the code.