zoukankan      html  css  js  c++  java
  • 求31000之间的素数

    #include <stdio.h>
    #include 
    <conio.h>
    #include 
    <math.h>
    void main()
    {
     clrscr();
     unsigned i,j,flag,count;
         
    /**//*用flag作标志*/
     count
    =0;
         
    /**//*计数器清0*/
     
    for(i=3;i<=1000;i++)
     
    {
      flag
    =0;
      
    for(j=2;j<=sqrt(i);j++)
      
    {
       
    if (i%j==0
         
    /**//*能整除,不是素数,标志设为1*/
       
    {flag=1;break;}
      }

         
    /**//*内循环结束*/
      
    if (flag==0)
         
    /**//*若flag为0,i是素数*/
      
    {
       printf(
    "%4d,",i);count++;
       
    if (count%8==0) printf("\n");
         
    /**//*每行输出8个素数*/
      }

     }
       /**//*外循环*/
    }

    第二种方法
    #include<stdio.h>
    #include
    <conio.h>
    #include
    <math.h>
    void main()
    {
        
    int i,n;
        
    for(i=2;i<=50;i++)
        
    {
            
    for(n=2;n<=(int)sqrt(i);n++)
                
    if(i%n==0)
                    
    break;
            
    if(n==(int)sqrt(i)+1)
                printf(
    "%d ",i);
        }

        printf(
    ".");
        getch();

    }
  • 相关阅读:
    正则表达式和re模块
    面向对象基础
    面向对象高级
    网络编程
    collectiontimeossysjson模块
    继承
    封装
    我在吃饭
    111
    关于羽毛球拍
  • 原文地址:https://www.cnblogs.com/qixin622/p/618920.html
Copyright © 2011-2022 走看看