這個是小孩子的作業,其實我以前也用 python 做過類似的練習,那時是找出某個數字以下的所有質數,這次只是判斷一個數字。
======================================================================
#include <iostream>
// 這個程式功用是判斷數字是否為質數,在數字不大的情況下,這應該是簡單度與效率的最好比例了。
using namespace std;
int main() {
unsigned int a,b,c,d;
//cout << "請輸入數字 \n";
//cin >> a;
//a=2147483647;
a=4294967291;
b=1;
d= a/2;
c=1;
//cout << c << endl;
while( b<d and c!=0)
{
b=b+1;
c=a%b;
}
if(c==0){
cout << "不是質數!\n";
}
else{
cout << "質數!\n";
}
cout << "程式結束!";
return 0;
}
================================================================
以下是執行結果:
================================================================
請輸入數字
2147483647
質數!
=================================================================
我對質數有些好奇,但研究的人實在太多了,意即我投入時間研究大概也很難有新發現,但或許未來有空,還是可以探索一下的。所以留下 C++ 讀取與輸出文字檔的範例連結給未來的自己。
讀取:https://shengyu7697.github.io/cpp-read-text-file/
寫入:https://shengyu7697.github.io/cpp-write-text-file/
另外是質數研究的介紹:https://math.ntnu.edu.tw/~horng/letter/hpm16011.pdf
沒有留言:
張貼留言