




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quá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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 教學課件展現(xiàn)方式
- 微體會活動方案
- 當幸福來敲門活動方案
- 開業(yè)活動立板活動方案
- 強兵富國活動方案
- 上海師范大學天華學院行政人員招聘考試真題2024
- 廣漢市招聘社區(qū)專職工作者考試真題2024
- 福建電力職業(yè)技術(shù)學院《名特蔬菜栽培》2023-2024學年第一學期期末試卷
- 湖南理工學院南湖學院《篆書基礎》2023-2024學年第一學期期末試卷
- 江蘇第二師范學院《工程測量學》2023-2024學年第一學期期末試卷
- SMW工法樁拔除技術(shù)交底
- 企業(yè)部門人員需求申請表
- 手太陰肺經(jīng)ppt課件
- 城市居住區(qū)規(guī)劃設計規(guī)范(含條文說明)
- (完整版)《普通心理學-彭聃齡》知識要點
- 借款擔保人擔保承諾書
- 口腔檢查與窩溝封閉記錄表
- 集團營銷管理辦法S
- 推拿療法治療青少年近視療效觀察
- 規(guī)劃設計條件告知書
- 數(shù)據(jù)結(jié)構(gòu)之赫夫曼編碼講解
評論
0/150
提交評論