欧拉工程第69题:Totient maximum
欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数。
当n小于1,000,000时候,n/φ(n)最大值时候的n。
这里的是p是n的素因子,当素因子有相同的时候只取一个
任意一个正整数都能分解成若干个素数乘积的形式
如下所示:
long phi(int n){
long res=0;
int pi=0;
if(n==1) return 0;
res = n;
pi = 2;
while(n!=1){
if(n%pi==0){
res*=(pi-1);
res/=pi;
while(n%pi==0){
n/=pi;
}
}
pi++;
}
return res;
}
上面res是存放φ(n)
是素因子的数更行res
由于素因子可能相同的
while(n%pi==0){
n/=pi;
}
这里的while循环就是用来去除重复的素因子
然而运行结果:
// 510510 5.539388020833333
// running time=270s483ms
这里要变量一百万次,内循环还要遍历,时间已经超过了欧拉工程的一分钟原则
参考网上程序,改成下面的形式:
long phi2(int n){
long res = 0;
if(n==1) return 0;
int pi=2;
res = n;
while(pi*pi <=n){
if(n%pi==0){
res*=(pi-1);
res/=pi;
while(n%pi==0){
n/=pi;
}
}
pi++;
}
if(n>1){
res*=(n-1);
res/=n;
}
return res;
}
一个结束条件是while(n!=1)
一个结束条件是while(pi*pi<=n) n的素因子一定小于等于根号n,当pi大于根号n的时候的循环完全是拜拜浪费时间
此时你一定想到素数的循环,这里很类似
运行结果:
// 510510 5.539388020833333
// running time=1s292ms
时间少了很多
在题解报告中,看到用到素数
当这个数的是素数的时候,欧拉函数φ(n) = n-1
当不是素数时候,找出小于n的素数,且能不n整除的数是n的素因子
long phi3(int n){
long res = n;
int pi=2;
if(isPrime(n)||n==1)
res = n-1;
else{
while(pi<=n){
if(n%pi==0 &&isPrime(pi)){
res*=(pi-1);
res/=pi;
}
pi++;
} }
return res; }
结果:
// 510510 5.539388020833333
// running time=1885s497ms
上面程序对找到的素因子,没有去除,同时循环是while(pi<=n),可以进一步优化
while(pi<=n){
if(n%pi==0 &&isPrime(pi)){
res*=(pi-1);
res/=pi;
while(n%pi==0){
n/=pi;
}
}
pi++;
}
结果:
// 510510 5.539388020833333
// running time=111s291ms
时间少了好多
while(pi*pi<=n){
if(n%pi==0 &&isPrime(pi)){
res*=(pi-1);
res/=pi;
// while(n%pi==0){
// n/=pi;
// }
}
pi++;
}
结果:
// 510510 5.539388020833333
// running time=4s531ms
然而while(pi*pi<=n) + 去除相同素因子 的,程序结果不对!!!
while(pi*pi<=n){
if(n%pi==0 &&isPrime(pi)){
res*=(pi-1);
res/=pi;
while(n%pi==0){
n/=pi;
}
}
pi++;
}
if(n>1) res = res/n*(n-1);
这样就对了
结果:
// 510510 5.539388020833333
// running time=1s454ms
去重后,最后一个n也是符合条件的
这个时间竟然比第2个的时间还要长。
Python程序:
import time as time def phi(n):
if n==1 :return 0
res = n
pi = 2
while(pi*pi<=n):
if n%pi==0:
res=res/pi*(pi-1)
while n%pi==0:
n/=pi
pi+=1
if n>1:res=res/n*(n-1)
return res
# 510510
# running time: 32.007999897 s
if __name__ == '__main__':
t0 = time.time()
Max_n = 1000000
result= 1
value = 0.0
for n in range(2,Max_n):
euler = phi(n)
temp = n/(euler*1.0)
if temp>value:
value = temp
result = n
print result
print "running time:",(time.time() - t0),'s'
全部的Java程序:
package project61; public class P69{ void run(){ long max_n = 1000000;
double value = 0.0;
long euler = 0;
long N=0; for(int i=2;i<=max_n;i++){ euler = phi3(i);
// System.out.println(i+" "+euler);
double temp = (double)i/(euler*1.0);
if(temp>value){
value = temp;
N = i;
} }
System.out.println(N+" "+value);
}
long phi3(int n){
long res = n;
int pi=2;
if(isPrime(n)||n==1)
res = n-1;
else{
while(pi*pi<=n){
if(n%pi==0 &&isPrime(pi)){
res*=(pi-1);
res/=pi;
while(n%pi==0){
n/=pi;
}
}
pi++;
}
if(n>1) res = res/n*(n-1);
}
return res; }
// 510510 5.539388020833333
// running time=1885s497ms // 510510 5.539388020833333
// running time=111s291ms // 510510 5.539388020833333
// running time=4s531ms // 510510 5.539388020833333
// running time=1s454ms
boolean isPrime(int num){
if(num==2||num==3||num==5||num==7) return true;
if(num<=1||num%2==0||num%3==0) return false;
for(int i=2;i<=Math.sqrt(num)+1;i++){
if(num%i==0) return false;
}
return true;
}
long phi2(int n){
long res = 0;
if(n==1) return 0;
int pi=2;
int k =0;
res = n;
while(pi*pi <=n){
if(n%pi==0){
res*=(pi-1);
res/=pi;
while(n%pi==0){
n/=pi;
}
}
pi++;
}
if(n>1){
res*=(n-1);
res/=n;
}
return res;
}
// 510510 5.539388020833333
// running time=1s292ms long phi(int n){
long res=0;
int pi=0;
if(n==1) return 0;
res = n;
pi = 2;
while(n!=1){
if(n%pi==0){
res*=(pi-1);
res/=pi;
while(n%pi==0){
n/=pi;
}
}
pi++;
}
return res;
}
// 510510 5.539388020833333
// running time=270s483ms public static void main(String[] args){
long start = System.currentTimeMillis();
new P69().run();
long end = System.currentTimeMillis();
long time = end - start;
System.out.println("running time="+time/1000+"s"+time%1000+"ms"); }
}
欧拉工程第69题:Totient maximum的更多相关文章
- 欧拉工程第67题:Maximum path sum II
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma ...
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- 欧拉工程第66题:Diophantine equation
题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...
- 欧拉工程第65题:Convergents of e
题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...
- 欧拉工程第74题:Digit factorial chains
题目链接:https://projecteuler.net/problem=74 数字145有一个著名的性质:其所有位上数字的阶乘和等于它本身. 1! + 4! + 5! = 1 + 24 + 120 ...
- 欧拉工程第56题:Powerful digit sum
题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...
- 欧拉工程第55题:Lychrel numbers
package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...
- 欧拉工程第54题:Poker hands
package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...
- 欧拉工程第53题:Combinatoric selections
package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...
随机推荐
- 使用MongoDB的开源项目
根据谷歌的搜索结果筛选出来的. 统计应用 counlty https://count.ly/ mongopress 开源CMS系统 http://www.mongopress.org/ Rubedo ...
- IBM MQ Reason 2538(MQRC_HOST_NOT_AVAILABLE) 错误原因一例
环境: .NET 4.0, MQ .NET客户端 IBM.XMS(v2.0.0.3) 测试代码如下: var factoryFactory = XMSFactoryFactory.GetInstanc ...
- .net speed up
Ngen.exe http://www.cnblogs.com/yukaizhao/archive/2011/11/07/how-to-use-ugen.html Merge.exe Merge dl ...
- Python 安装 httpie
Python 安装 httpie 前段时间开发RESTful的程序,使用浏览器插件HttpRequester,挺高级,易用的.后来在RESTHeart项目中认识了httpie,感觉高大上.在使用htt ...
- [转载]C# FTP操作工具类
本文转载自<C# Ftp操作工具类>,仅对原文格式进行了整理. 介绍了几种FTP操作的函数,供后期编程时查阅. 参考一: using System; using System.Collec ...
- SVN 安装配置
1,软件下载 到官方网站的下载二进制安装文件,来到二进制包下载部分,找到 Windows NT, 2000, XP and 2003部分,然后选择Apache 2.2 或者 Apache 2.4,这样 ...
- spring配置事务
一.配置JDBC事务处理机制 <!-- 配置Hibernate事务处理 --> <bean id="transactionManager" class=" ...
- 【BZOJ】【2594】【WC2006】水管局长数据加强版
LCT 动态维护MST嘛……但是有删边= =好像没法搞的样子 离线记录所有修改&询问,倒序处理,就可以变删边为加边了- 论如何用LCT维护最小生成树:先搞出一棵最小生成树,然后每次加边(u,v ...
- 【BZOJ】【1430】小猴打架
排列组合 蛮逗的…… 这题题干描述的就一股浓浓的Kruskal的气息……很容易就想到是求一个n个点的完全图的生成树个数,然后由于有序,再乘一个n-1的排列数(n-1条边的全排列)即(n-1)! 但是我 ...
- nenu contest3 The 5th Zhejiang Provincial Collegiate Programming Contest
ZOJ Problem Set - 2965 Accurately Say "CocaCola"! http://acm.zju.edu.cn/onlinejudge/showP ...