# 一個計算質數的小程式

http://true5460.muicc.com/other/calPN.zip

using System;

class test
{
static void Main() {
ulong i;
while(true) {
Console.Write("請輸入要計算的質數（0退出）：");
if(i==0) break;
DateTime t1 = DateTime.Now;
switch(i) {
case 1:Console.WriteLine("1 不是質數！");break;
case 2:Console.WriteLine("2 是質數！");break;
default:cal(i);break;
}
DateTime t2 = DateTime.Now;
Console.WriteLine("時間為：{0} 毫秒\n",(t2-t1).Ticks/10000f);
}
}

//以下為函數部分

static void cal(ulong x) {
ulong sum=1;
byte row=1;
Console.Write("\n");
for(ulong a=3;a<x+1;a++) {
bool flag=true;
for(ulong b=2;b<(a/2)+1;b++) { if(a%b!=0) continue;
flag=false;
break;
}
if(flag) {
if(row==10) { Console.WriteLine();row=0; }
if(sum==1) Console.Write("{0,7}",2);
Console.Write("{0,7}",a);
sum++;row++;
}

}
Console.WriteLine("\n\n{0} 以內共有 {1} 個質數\n",x,sum);

}

}

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD