Skip to content

Instantly share code, notes, and snippets.

@HyodaKazuaki
Created January 12, 2017 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HyodaKazuaki/07e7122454f2a992f7766aabe8a12f81 to your computer and use it in GitHub Desktop.
Save HyodaKazuaki/07e7122454f2a992f7766aabe8a12f81 to your computer and use it in GitHub Desktop.
並列化した素数計算プログラム
Console.Write("求めたい値を入力 >");
string target_str = Console.ReadLine();
long target = long.Parse(target_str);
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
bool flg = false;
sw.Start();
Parallel.For(2, 8202464, (i, loopstate) => {
if(target % i == 0)
{
flg = true;
loopstate.Break();
}
});
sw.Stop();
if (flg)
{
Console.WriteLine(target + "は素数ではない");
Console.WriteLine("処理時間" + sw.ElapsedMilliseconds + "ms");
}
else
{
Console.WriteLine(target + "は素数");
Console.WriteLine("処理時間" + sw.ElapsedMilliseconds + "ms");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment