判断是不是素数

 1 #include "stdafx.h"
 2 
 3 int isPrime(long n)
 4 {
 5     long a;
 6     a = 2;
 7     while (a < n)
 8     {
 9         if (n%a == 0)
10             break;
11         a++;
12     }
13     if (a == n)
14         return 1;
15     else
16         return 0;
17 }
18 
19 int main()
20 {
21     //while (scanf_s("%ld", &n) != EOF)
22     //{
23     //}
24     int num = 0;
25     long length = 100;
26     for (long i = 1; i < length; i++)
27     {
28         if (isPrime(i) == 1)
29         {
30             printf("%d ", i);
31             num++;
32         }
33     }
34     printf("\n在%d的数字中有%d个素数如上\n", length, num);
35     return 0;
36 }

猜你喜欢

转载自www.cnblogs.com/kongchung/p/9330378.html