Skip to content

Instantly share code, notes, and snippets.

@HyodaKazuaki
Last active January 12, 2017 09:22
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/5d48eb7bafb8592c08e7bf1b9c660060 to your computer and use it in GitHub Desktop.
Save HyodaKazuaki/5d48eb7bafb8592c08e7bf1b9c660060 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();
long i = 2;
for (i = 2; i < target / i; i++)
{
if(target % i == 0)
{
flg = true;
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