版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- T/HZESA 001-2021浙江电化学储能电站并网技术规范
- T/DZJN 430-2025绿色产品评价 保健按摩垫
- T/CHES 131-2024一体化翻斗式雨量计
- T/CADERM 7006-2021多模态呼吸器
- T/CASME 1926-2025液流电池用碳毡电极技术规范
- T/CACEM 56-2025港口企业班组建设指南
- DB32/T 5064-2025功能性肛门直肠痛中医诊疗规范
- 生产计划编制流程SOP-含计划表和产能分析表
- T/CACEC 0004-2024管道伴行光缆光纤在线监测系统设计及施工规范
- T/CASME 1866-2024绿色建筑机电末端安装工程技术规范
- 2026(青岛版新教材)数学三年级上册全册数学教学设计
- 失能老年人健康评估服务工作细则
- 2026南方基金金融科技专场校园招聘笔试历年难易错考点试卷带答案解析
- 西北工业大学附属中学2026届高三下学期第十一次适应性训练数学试卷
- 学校食堂留样柜双人双锁工作制度
- 水厂安全保卫工作制度
- 从零开始学量价分析(短线操盘-盘口分析与A股买卖点实战)
- 2026年湖南省高职单招职业适应性测试考试题库及答案解析
- 躯体症状障碍的诊疗与心理干预
- 公路工程监理培训制度
- 上海市崇明区崇明中学2026届高一数学第一学期期末检测模拟试题含解析
评论
0/150
提交评论