問題描述:
尤拉公式: a(n) = n^2 - n + 41,求n在 -39 ~ 40範圍內的素數。
演算法分析:
窮舉法,窮舉出-39~40範圍內的a(n),再判斷a(n)是否為素數;
**如下:
view code
1 #include輸出結果:2 #include 34
int main(void)
5 19
20 }
2122
if(flag == 0) //
判斷標記來判斷是否是素數
23
2728 }
29 }
-39 -> 1601 is prime
-38 -> 1523 is prime
-37 -> 1447 is prime
-36 -> 1373 is prime
-35 -> 1301 is prime
-34 -> 1231 is prime
-33 -> 1163 is prime
-32 -> 1097 is prime
-31 -> 1033 is prime
-30 -> 971 is prime.
-29 -> 911 is prime.
-28 -> 853 is prime.
-27 -> 797 is prime.
-26 -> 743 is prime.
-25 -> 691 is prime.
-24 -> 641 is prime.
-23 -> 593 is prime.
-22 -> 547 is prime.
-21 -> 503 is prime.
-20 -> 461 is prime.
-19 -> 421 is prime.
-18 -> 383 is prime.
-17 -> 347 is prime.
-16 -> 313 is prime.
-15 -> 281 is prime.
-14 -> 251 is prime.
-13 -> 223 is prime.
-12 -> 197 is prime.
-11 -> 173 is prime.
-10 -> 151 is prime.
-9 -> 131 is prime.
-8 -> 113 is prime.
-7 -> 97 is prime.
-6 -> 83 is prime.
-5 -> 71 is prime.
-4 -> 61 is prime.
-3 -> 53 is prime.
-2 -> 47 is prime.
-1 -> 43 is prime.
0 -> 41 is prime.
1 -> 41 is prime.
2 -> 43 is prime.
3 -> 47 is prime.
4 -> 53 is prime.
5 -> 61 is prime.
6 -> 71 is prime.
7 -> 83 is prime.
8 -> 97 is prime.
9 -> 113 is prime.
10 -> 131 is prime.
11 -> 151 is prime.
12 -> 173 is prime.
13 -> 197 is prime.
14 -> 223 is prime.
15 -> 251 is prime.
16 -> 281 is prime.
17 -> 313 is prime.
18 -> 347 is prime.
19 -> 383 is prime.
20 -> 421 is prime.
21 -> 461 is prime.
22 -> 503 is prime.
23 -> 547 is prime.
24 -> 593 is prime.
25 -> 641 is prime.
26 -> 691 is prime.
27 -> 743 is prime.
28 -> 797 is prime.
29 -> 853 is prime.
30 -> 911 is prime.
31 -> 971 is prime.
32 -> 1033 is prime.
33 -> 1097 is prime.
34 -> 1163 is prime.
35 -> 1231 is prime.
36 -> 1301 is prime.
37 -> 1373 is prime.
38 -> 1447 is prime.
39 -> 1523 is prime.
40 -> 1601 is prime.
求規定範圍內的完美數
輸入 檔名稱 sum123.cpp 作 者 林海雲 完成日期 2014年12月24日 版 本 號 v2.0 問題描述 求規定範圍內滿足條件的完美數。6的所有真約數是1 2 3,而且6 1 2 3。像這樣,乙個數所有真約數的和正好等於這個數,通常把這個數叫做完美數 程式輸入 m x n且輸入0 0結束...
求乙個範圍內的素數
學演算法時候,求素數總是一道逃不掉的練習題。好久沒寫演算法相關的練習了,學習了python 就拿它來練一下手吧。在求素數之前,首先我們了解一下 什麼是素數。按維基百科的說法是 素數指在大於1的自然數中,除了1和此整數自身外,無法被其他自然數整除的數 也可定義為只有1和本身兩個因數的數 因此我們可以總...
求指定範圍內的最大數
同學們都知道,無論哪個語言入門學習時,第乙個sample都是 hello world!我們學習演算法,第乙個入門的問題也許就是求給定輸入的最大或者最小,那麼我們今天也來求解這個問題,只不過我們今天需要你求的是指定範圍內的最大值。input 本問題有多組測試資料,每一組有兩行,其中第一行有三個整數n,...