Skip to content

Instantly share code, notes, and snippets.

@donma
Last active October 29, 2025 07:28
Show Gist options
  • Select an option

  • Save donma/3baa493d7ed06da62b7acf28cc16e279 to your computer and use it in GitHub Desktop.

Select an option

Save donma/3baa493d7ed06da62b7acf28cc16e279 to your computer and use it in GitHub Desktop.
/// <summary>
/// 簡單判斷是不是質數,反正在10以內,不可能太久
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
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;
}
//計時器,非必要
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment