快速尋找素數

來源:互聯網
上載者:User

標籤:

快速尋找素數時間限制:1000 ms  |  記憶體限制:65535 KB難度:3
描述
現在給你一個正整數N,要你快速的找出在2.....N這些數裡面所有的素數。
輸入
給出一個正整數數N(N<=2000000)
但N為0時結束程式。
測試資料不超過100組
輸出
將2~N範圍內所有的素數輸出。兩個數之間用空格隔開
範例輸入
510110
範例輸出
2 3 52 3 5 72 3 5 7 11
來源
經典題
上傳者

路過這

素數打表。提交開始逾時。改成c語言的就過了

 #include<iostream>#include<string.h>#include<math.h>#include<stdio.h>using namespace std;int a[2000001];void sushu(){int i,j;a[0]=a[1]=1;for(i=2;i<=sqrt(2000000);i++){if(a[i]==0){for(j=i*i;j<=2000000;j+=i){a[j]=1;}}}}int main(){sushu();int N,i,time;while(scanf("%d",&N) && N){time=0;if(N>=2)   printf("2");         for(i=3;i<=N;i++) { if(a[i]==0) //cout<<" "<<i;printf(" %d",i); } cout<<endl;}return 0;}         

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

快速尋找素數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.