PAT 1013. 數素數 (20)

來源:互聯網
上載者:User

標籤:表示   printf   number   stdio.h   sqrt   str   ber   bsp   格式   

令Pi表示第i個素數。現任給兩個正整數M <= N <= 104,請輸出PM到PN的所有素數。

輸入格式:

輸入在一行中給出M和N,其間以空格分隔。

輸出格式:

輸出從PM到PN的所有素數,每10個數字佔1行,其間以空格分隔,但行末不得有多餘空格。

輸入範例:

5 27

輸出範例:

11 13 17 19 23 29 31 37 41 4347 53 59 61 67 71 73 79 83 8997 101 103
 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 int main(){ 5     int n,m; 6     scanf("%d %d",&n,&m); 7     int sum = 0; 8     int i = 1; 9     int j;10     int k = 0;11     int number[10001];12     while(i++){13         for(j=2;j<=(int)sqrt(i);j++){14             if(i%j==0)15                 break;16         }17         if(j>(int)sqrt(i)){18             sum++;    19             if(sum>=n&&sum<=m){20                 if((sum-n+1)%10==1){21                     printf("%d",i);22                 }23                 else24                     printf(" %d",i);25                 if((sum-n+1)%10==0){26                     printf("\n");27                 }28             }29         }30         if(sum>m)31             break;32     }33 }

 

PAT 1013. 數素數 (20)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.