题目链接

欧拉函数φ(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的更多相关文章

  1. 欧拉工程第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 ...

  2. 欧拉工程第70题:Totient permutation

    题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...

  3. 欧拉工程第66题:Diophantine equation

    题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...

  4. 欧拉工程第65题:Convergents of e

    题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...

  5. 欧拉工程第74题:Digit factorial chains

    题目链接:https://projecteuler.net/problem=74 数字145有一个著名的性质:其所有位上数字的阶乘和等于它本身. 1! + 4! + 5! = 1 + 24 + 120 ...

  6. 欧拉工程第56题:Powerful digit sum

    题目链接   Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...

  7. 欧拉工程第55题:Lychrel numbers

    package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...

  8. 欧拉工程第54题:Poker hands

    package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...

  9. 欧拉工程第53题:Combinatoric selections

    package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...

随机推荐

  1. Less 导入命令 @import

    在这个less文件上想导入另一个less文件, 连在同级的文件里直接可用文件名 @import url('css.less')或@import rul(css) 连下级的文件 @import url( ...

  2. Ping N个IP测试网络连通性

    #-----------------------Smokeping移动节点-------------------##! /bin/bashecho "------------- Statin ...

  3. Lucene Field

    org.apache.lucene.demo.IndexFiles类中,使用递归的方式去索引文件.在构造了一个IndexWriter索引器之后,就可以向索引器中添加Doucument了,执行真正地建立 ...

  4. PHP 图片文件上传代码

    通过 PHP,可以把文件上传到服务器.里面加入一些图片的判断,如果不加判断文件的类型就可以上传任意格式的文件. 为了网站的安全,肯定不让上传php文件,如果有人进入你的后台,上传了一个php文件,你的 ...

  5. 使用cookie解决微信不能存储localStorage的问题

    最近在开发基于微信的Web页面时,发现有些机型不能存储信息到localStorage中,或者是页面一旦关闭,存储的信息也失效了. 于是想到用cookie来替代localStorage,存储一些简单的数 ...

  6. javascript中的省市级联效果

    学习javascript的时候都遇到过这样的需求,不仅是省市,还有其他的一些场景,看看关键的代码有哪些吧. <head runat="server"> <titl ...

  7. SetTimeOut jquery的作用

    1. SetTimeOut() 1.1 SetTimeOut()语法例子 1.2 用SetTimeOut()执行Function 1.3 SetTimeOut()语法例子 1.4 设定条件使SetTi ...

  8. RaddioButton控件

    <GroupBox Margin="5"> <StackPanel> <RadioButton IsChecked="true"& ...

  9. 【jquery】 API讲解 内部培训资料

    资料在百度云盘 一.jquery  API讲解 1.jquery  api如何使用 jquery  api http://www.hemin.cn/jq/ 2.常用api讲解 选择器: 通过$()获取 ...

  10. apache-tomcat-9安装以及与eclipse结合

    apache-tomcat-下载:http://tomcat.apache.org/download-90.cgi 安装(转载):http://jingyan.baidu.com/article/60 ...