




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、583 - Prime Factors99703032 資科二 陳宣耀99703034 資科二 鍾佳樺99703045 資科二 陳乃藩Outline Primality Test Prime Table Prime FactorsPrimality Testint isPrime_s1a(int n) int m; if(n=1) return 0; for(m=2;m 2s for multi-input in one of OJs. How to modify?Primality Testevery even number except 2 is not a prime number.in
2、t isPrime_s1b(int n) int m; if(n=2) return 1; if(n=1)|(n%2=0) return 0; for(m=3;m 2s for multi-input in the same OJ. How to modify?Primality Test Theorem: if x mod n 0 for all 2 x , then n is a prime number. Because that there exist no prime number of n which is larger then .nnPrimality Testint isPr
3、ime_s1c(int n) int m,sqrt_n=sqrt(n); if(n=2) return 1; if(n=1)|(n%2=0) return 0; for(m=3;m=sqrt_n;m+=2) if(n%m=0) return 0; return 1;Primality Test Time complexity = ( ) Running time 0.20s for multi-input in the OJ. Advantage: a fast solution for one input. Any other solution?nPrime Table Eratosthen
4、es: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 2 1 2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 3 1 2 3 5 7 11 13 17 19 23 25 29 31 5 1 2 3 5 7 11 13 17 19 23 29 31 . . .Prime Table Eratosthenes: void eratosthenes(int sieve,int size) int i,j,sqrt_size=sqrt(size
5、); sieve0=sieve1=0; for(i=2;isize;+i) sievei=1; for(i=2;i=sqrt_size;+i) if(sievei=1) for(j=i*i;jsize;j+=i) sievej=0; Prime Table Build a prime table from the sieve: int table(int prime, const int sieve, int sizeOfSieve) int i,j=0; for(i=0;isizeOfSieve;+i) if(sievei=1) primej+=i; return j; Prime Tabl
6、e Time complexity = O( n log(log n) ) n is the size of sieve. Usage: Primality Test FactorizationPrime Table Use prime table for Primality Test: int isPrime_s2a(int n,int prime) int i,sqrt_n=sqrt(n); if(n=1) return 0; for(i=0;primei1 is said to be prime if and only if its only positive divisors are
7、itself and one (otherwise it is said to be composite). For example, the number 21 is composite; the number 23 is prime. Note that the decompositon of a positive number g into its prime factors, i.e., is unique if we assert that fi 1 for all i and for ij.One interesting class of prime numbers are the
8、 so-called Mersenne primes which are of the form 2p- 1. Euler proved that 231 - 1 is prime in 1772 - all without the aid of a computer.Prime FactorsInputThe input will consist of a sequence of numbers. Each line of input will contain one number g in the range -231 g 231, but different of -1 and 1. T
9、he end of input will be indicated by an input line having a value of zero.OutputFor each line of input, your program should print a line of output consisting of the input number and its prime factors. For an input number , where each fi is a prime number greater than unity (with for ij), the format
10、of the output line should beWhen g 0, if , the format of the output line should bePrime FactorsSample Input-190-191-192-193-1941951961971981992000Sample Output-190 = -1 x 2 x 5 x 19-191 = -1 x 191-192 = -1 x 2 x 2 x 2 x 2 x 2 x 2 x 3-193 = -1 x 193-194 = -1 x 2 x 97195 = 3 x 5 x 13196 = 2 x 2 x 7 x
11、7197 = 197198 = 2 x 3 x 3 x 11199 = 199200 = 2 x 2 x 2 x 5 x 5Prime Factors#include#includeint buildTable(int prime,int sizeOfSieve);int factor(int num,int output,const int table,int sizeOfPrime);int main(void) const int sizeOfArray=sqrt(pow(2,31)-1); int primesizeOfArray,sizeOfPrime; int outputsize
12、OfArray,sizeOfOutput; int input,i; sizeOfPrime=buildTable(prime,sizeOfArray); while(scanf(%d,&input)=1) if(input=0) break; sizeOfOutput=factor(input,output,prime,sizeOfPrime); printf(%d = %d,input,output0); for(i=1;isizeOfOutput;+i) printf( x %d,outputi); printf(n); return 0;/*continue*/Prime Fa
13、ctorsvoid sieveOfEratosthenes(int sieve,int size) int i,j,sqrt_size=sqrt(size); sieve0=sieve1=0; for(i=2;isize;+i) sievei=1; for(i=2;i=sqrt_size;+i) if(sievei=1) for(j=i*i;jsize;j+=i) sievej=0;int buildTable(int prime,int sizeOfSieve) int i,j=0; sieveOfEratosthenes(prime,sizeOfSieve); for(i=0;isizeOfSieve;+i) if(primei=1) primej+=i; return j;/*continue*/Prime Factorsint factor(int num,int output,const int table,int sizeOfPrime) int i,sqrt_num,sizeOfOutput=0; if(num0) outputsizeOfOutput+=-1; num*=-1; else if(num=1) output0=1; ret
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度床上用品綠色包裝設(shè)計(jì)與實(shí)施合同
- 2025年度勞動(dòng)電子合同電子合同數(shù)據(jù)安全保密協(xié)議
- 2025年度年會(huì)抽獎(jiǎng)活動(dòng)設(shè)計(jì)與執(zhí)行合同
- 2025年度合伙經(jīng)營(yíng)連鎖蛋糕店品牌授權(quán)與區(qū)域保護(hù)協(xié)議
- 2025年度新型建筑材料貿(mào)易合作合同范本
- 中國(guó)廣播電視設(shè)備制造市場(chǎng)深度分析及投資戰(zhàn)略咨詢報(bào)告
- 社區(qū)養(yǎng)老調(diào)研報(bào)告范文
- 項(xiàng)目進(jìn)度檢查計(jì)劃
- 中國(guó)電競(jìng)酒店行業(yè)市場(chǎng)深度分析及投資潛力預(yù)測(cè)報(bào)告
- 2025年中國(guó)擴(kuò)散焊機(jī)行業(yè)發(fā)展監(jiān)測(cè)及發(fā)展趨勢(shì)預(yù)測(cè)報(bào)告
- 羽毛球教案18課時(shí)
- 初三化學(xué)一輪復(fù)習(xí)計(jì)劃
- 鏈家新人成長(zhǎng)手冊(cè)10
- 成人重癥患者人工氣道濕化護(hù)理專家共識(shí) 解讀
- 新版蘇教版六年級(jí)數(shù)學(xué)上冊(cè)全冊(cè)解析
- JTT 1499-2024 公路水運(yùn)工程臨時(shí)用電技術(shù)規(guī)程(正式版)
- 2024年甘肅省天水市中考生物·地理試題卷(含答案)
- 壓力變送器的拆卸及安裝 壓力變送器維護(hù)和修理保養(yǎng)
- 2024遼寧大連中遠(yuǎn)海運(yùn)川崎船舶工程限公司招聘73人公開(kāi)引進(jìn)高層次人才和急需緊缺人才筆試參考題庫(kù)(共500題)答案詳解版
- 2024年上海市法院系統(tǒng)輔助文員招聘筆試參考題庫(kù)附帶答案詳解
- 企業(yè)復(fù)產(chǎn)復(fù)工方案
評(píng)論
0/150
提交評(píng)論