求从1到1000之间的素数

问题

求从1到1000之间的素数

分析

穷举

解决方案

 1:  /**
 2:   * @file   030c.c
 3:   * @author Chaolong Zhang <emacsun@163.com>
 4:   * @date   Sat Jun  1 23:59:05 2013
 5:   * 
 6:   * @brief  求从1到1000之间的素数
 7:   */
 8:  
 9:  #include <stdio.h>
10:  
11:  #define N 1000
12:  
13:  int main(int argc, char *argv[])
14:  {
15:      int  n,i,flag;
16:  
17:      for (n=4; n <= N; ++n)
18:      {
19:          flag=1;
20:  
21:          for ( i = 2; i <= ( n/2 + 1 ); ++i) 
22:              if (n%i == 0 ) {
23:                  flag=0;break;
24:              }
25:          if (flag) printf ("%d ",n);
26:      }
27:      return 0;
28:  }

输出结果

posted @ 2013-06-02 23:04  emacsun  阅读(2315)  评论(0编辑  收藏  举报