Last active
October 29, 2025 07:28
-
-
Save donma/3baa493d7ed06da62b7acf28cc16e279 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <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; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //計時器,非必要 | |
| 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