The largest prime factor(最大质因数)
1. 问题:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
2. 解法(by java in Eclipse)
package com.lun.alrithmetic;
/*
* Q1: what's the primary factor? (1 2 3)
* Q2: i & i+2ne能否遍历出所有质数
*/ public class LargestPrimeFactor { public static void main(String[] args) {
// TODO Auto-generated method stub
long n = 600851475143L;
System.out.println("The largest prime factor of the number '600851475143' is "+largestPrimeFactor(n));
} public static boolean isPrime(long n){
if(n<4)
return n>1;
if(n%2==0 || n%3==0)
return false;
for(long i=5; i*i<n; i+=6)
if(n%i==0 || n%(i+2)==0)
return false;
return true;
} public static long largestPrimeFactor(long n){
if(n%2==0){
while(n%2==0)
n /= 2;
}
if(n%3==0){
while(n%3==0)
n /= 3;
}
for(int i=5; i*i<n; i+=6){
if(n%i==0){
while(n%i==0)
n /= i;
}
int j = i+2;
if(n%j==0){
while(n%j==0)
n /= j;
}
}
return n;
}
}
way1
package com.lun.alrithmetic;
/*
* Question: What's the largest prime factor of the number '600851475143' ?
* Function: Divide & Conquer, analyse prime factor
*/ public class LargestPrimeFactor2 { public static void main(String[] args) {
// TODO Auto-generated method stub
long n = 600851475143L;
System.out.println("The largest prime factor of the number '600851475143' is "+largest(n));
} public static boolean isPrime(long n){
if(n<4)
return n>1;
if(n%2==0 || n%3==0)
return false;
for(long i=5; i*i<n; i+=6)
if(n%i==0 || n%(i+2)==0)
return false;
return true;
} public static long largest(long n){
if(isPrime(n))
return n;
else{
if(n%2==0)
return largest(n/2);
if(n%3==0)
return largest(n/3);
for(long i=5; i*i<n; i+=6){
if(n%i==0)
return largest(n/i);
if(n%(i+2)==0)
return largest(n/(i+2));
}
} return 1;
} }
way2
注:利用分解质因数的方法,从小向大用质数整除(如果此质数恰好是n的因数的话)n,即不断的减小n的规模,最后即可求的最大质因数。
法二,采用了递归的方法易于理解,但是内存消耗较大;建议用法一。
The largest prime factor(最大质因数)的更多相关文章
- R语言学习——欧拉计划(3)Largest prime factor 求最大质因数
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60085 ...
- 【沙茶了+筛选保存最大质因数】【HDU2136】Largest prime factor
Largest prime factor Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Largest prime factor
problem 3:Largest prime factor 题意:求600851475143的最大的质因数 代码如下: #ifndef PRO3_H_INCLUDED #define PRO3_H_ ...
- [暑假集训--数论]hdu2136 Largest prime factor
Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...
- HDOJ(HDU) 2136 Largest prime factor(素数筛选)
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...
- (Problem 3)Largest prime factor
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60085 ...
- 2136 Largest prime factor(打表)
Problem Description Everybody knows any number can be combined by the prime number.Now, your task is ...
- Problem 3: Largest prime factor
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60085 ...
- 【ACM】Largest prime factor
/*打表把素数能组合的数先设置成相应的位数*/ /* if n equals two and n is No.1 position of prime factors so four position ...
随机推荐
- iOS UITextField 输入字数限制的实现
首先你的ViewController需要实现 UITextFieldDelegate 代理, 其次,需要字数限制的UITextField实例的代理要设置成 self(ViewController) 然 ...
- rails + mongoid 使用
1. 测试环境 2. 创建工程 rails new mongoid_app --skip-active-record --skip-test-unit --skip-bundle 3. 修改gemfi ...
- Android_设备隐私获取,忽略6.0权限管理
1.前言 (1).由于MIUI等部分国产定制系统也有权限管理,没有相关api,故无法判断用户是否允许获取联系人等隐私.在Android 6.0之后,新增权限管理可以通过官方api判断用户的运行状态: ...
- 3 Ways of JDK Source Code Attachment in Eclipse---reference
You wanna look at a JVM class while you are coding and you cannot. Here is the solution. First of al ...
- marquee 标签的使用详情
<marquee>标签,它是成对出现的标签,首标签<marquee>和尾标签</marquee>之间的内容就是滚动内容.<marquee>标签的属性主要 ...
- android自定义控件之模仿优酷菜单
去年的优酷HD版有过这样一种菜单,如下图: 应用打开之后,先是三个弧形的三级菜单,点击实体键menu之后,这三个菜单依次旋转退出,再点击实体键menu之后,一级菜单会旋转进入,点击一级菜单,二级菜单旋 ...
- RedHat7搭建MongoDB
yum安装MongoDB 添加MongoDB源# vi /etc/yum.repos.d/mongodb-org-3.0.repo [mongodb-org-3.0] name=MongoDB Rep ...
- JavaScript小笔记の经典算法等....
1.利用toString()里面的参数,实现各进制之间的快速转换: var n = 17; binary_string = n.toString(2); //->二进制"10001&q ...
- “System.Transactions.Diagnostics.DiagnosticTrace”的类型初始值设定项引发异常。
今天在项目中用log4net,App.config文件中增加了configSections节点,程序运行报错“System.Transactions.Diagnostics.DiagnosticTra ...
- [Excel] CsvHelper---C#关于CSV文件的导入和导出以及转化 (转载)
点击下载 CsvHelper.rar 这个类是关于Csv文件的一些高级操作1.DataTable导出到CSV2.将Csv读入DataTable看下面代码吧 /// <summary> // ...