题目链接

The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power.

How many n-digit positive integers exist which are also an nth power?

这个题目有点坑:

先说自己的思路<虽然方法不是很好>

根据题意可知道:

a的b次方 除以 最小的b位数(如:1,10,100,1000) 的商 在 1--9之间,则:a的b次方就是符合题意的

然后就根据这个遍历

先找到第一个数符合条件的数firstnum

再找到第一个符合条件之后的第一个不满足条件的数nextnum

则:这中间有 nextnum - firstnum个数

当b也就是次方数大于18的时候,Long都溢出了

此时:有46个数

下面是程序 :

 package project61;

 import java.math.BigInteger;

 public class P63{
// a的b次方是b位数
// a的b次方 除以 b位的第一个数(如:1000) 商 在1 - 9之间
// 以a为开始,找到第一个满足条件的数,如不存在返回 0
// 满足条件的数是连续的
long findFirst(long Base,int exp){
long res =(long) Math.pow(Base, exp);
long d = 1;
int Max_Cycle = 10000;
int texp = exp;
while(exp!=1){
d = d*10;
exp--;
}
boolean flag = true ;
int quot = 0;
while(Max_Cycle!=0){
quot = (int) (res/d);
// System.out.println(quot+"res:"+res+" Base:"+Base+" d:"+d);
if(quot>=1 && quot<=9){
return Base;
}
Base = Base + 1;
res = (long) Math.pow(Base, texp);
Max_Cycle--;
}
return 0 ;
} long findNext(long Base,int exp){
long res =(long) Math.pow(Base, exp);
long d = 1;
int Max_Cycle = 100000;
int texp = exp;
while(exp!=1){
d = d*10;
exp--;
}
boolean flag = true ;
int quot = 0;
while(Max_Cycle!=0){
quot = (int) (res/d);
System.out.println("商:"+quot +" 被除数:"+ res+" 除数:"+d+" 底数:"+Base+" 指数:"+texp);
if(quot==0 ||quot>9){ return Base;
}
Base = Base + 1;
res = (long) Math.pow(Base, texp);
Max_Cycle--;
}
return 0 ;
}
void run(){
long result = 0;
int base = 1;
int exp = 1; while(exp<=18){
base = 1 ;
long firstNum = findFirst(base,exp);
if(firstNum !=0){ long next = findNext(firstNum,exp);
System.out.println("第一个满足条件的底数:"+firstNum +" 第一个不满足条件的底数: "+ next);
result = result + next - firstNum; }
exp++;
}
System.out.println(result);
} public static void main(String[] args){
long begin= System.currentTimeMillis();
new P63().run();
long end = System.currentTimeMillis();
long Time = end - begin;
System.out.println("Time:"+Time/1000+"s"+Time%1000+"ms");
}
}

程序流程:

1.在相同的指数情况小,找符合条件的数

 1.1找到第一个符合条件的数的底数

 1.2找到符合条件数后面的第一个不符合条件的数

 1.3这两个数的差,就是在这个指数下所以符合条件的数

2.增加指数。

下面是运行的结果:

可以看出,只有1-9的底数满足条件,上面红框中的是满足条件后的第一个不满足条件的数。

上面至少知道18,设成18以上,溢出,,,输入上面的46当然是不对的,应该是还有的

尝试直接在9的19,20,21...的数中找符合条件的数:

        BigInteger base = new BigInteger("9");
BigInteger bigres = new BigInteger("0");
String toStr = "";
int exp = 18; for(exp=19;exp<25;exp++){
bigres = base.pow(exp);
toStr = bigres+"";
if(toStr.length() ==exp)
System.out.println(toStr);
}

通过上面的程序,就只是把符合条件的其他三个数输出了。

答案是:49

上面的过程是不是太复杂了,如果直接利用BigInteger也不会这么复杂的。

然后看到别人是这样做的:

上面说的很详细。。。

void run1(){
int count = 0;
for(int x = 1;x<10;x++){
count +=(int)(1.0/(1-Math.log(x)/Math.log(10))); }
System.out.println(count);
}

程序就成这样的了。。。

Python程序:

from math import log10

s = 0
for n in range(1,10):
s += int(1/(1-log10(n))) print "result=",s

Python程序就是这样的了。。。


from math import log10
print sum(map(int, map(lambda a: 1.0/(1.0-log10(a)), range(1, 10))))

Python 也可以这样来。。。

欧拉工程第63题:Powerful digit counts的更多相关文章

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

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

  2. 欧拉工程第69题:Totient maximum

    题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...

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

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

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

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

  5. 欧拉工程第51题:Prime digit replacements

    题目链接 题目: 通过置换*3的第一位得到的9个数中,有六个是质数:13,23,43,53,73和83. 通过用同样的数字置换56**3的第三位和第四位,这个五位数是第一个能够得到七个质数的数字,得到 ...

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

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

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

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

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

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

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

随机推荐

  1. 运行yum报错Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again

    今天给Centos通过rpm -Uvh装了个epel的扩展后,执行yum就开始报错: Error: Cannot retrieve metalink for repository: epel. Ple ...

  2. php学习日志(4)-The mbstring extension is missing. Please check your PHP configuration错误及解决方法

    在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring extension is missing. Please che ...

  3. 三种找回 linux root密码的方法

    找回 linux root密码的三种方法 第1种方法: 1.在系统进入单用户状态,直接用passwd root去更改2.用安装光盘引导系统,进行linux rescue状态,将原来/分区挂接上来,作法 ...

  4. Oracle利用数据泵迁移用户

    一.利用数据泵将数据导出 1.1.确定字符集: select * from v$nls_parameters; 或 select userenv('language') from dual; 1.2. ...

  5. 在Java中执行js代码

    在某些特定场景下,我们需要用Java来执行Js代码(如模拟登录时,密码被JS加密了的情况),操作如下: ScriptEngineManager mgr = new ScriptEngineManage ...

  6. 开发流程习惯的养成—TFS简单使用

    才开始用,所以是个很基础的介绍,欢迎大家一起交流学习 一.追本溯源 讲到开发流程,还要从敏捷开始,因为敏捷才有了开发流程的重视,整个流程也是按照敏捷的思想进行的,这里不再叙述敏捷的定义 敏捷的流程(个 ...

  7. java之javadoc命令

    [javadoc命令的用法] 1.java源文件里,注释以/**开始 并以*/结束,里面可以包含普通文件,HTML标记和javaDoc标记.这些将构成javaDoc文档. 2.javadoc命令只能处 ...

  8. 用Redis bitmap统计活跃用户、留存

    Spool的开发者博客,描述了Spool利用Redis的bitmaps相关的操作,进行网站活跃用户统计工作. 原文:http://blog.getspool.com/2011/11/29/fast-e ...

  9. windows2003通过iis配置ftp服务器

    以前习惯于用filezilla作为windows的ftp服务器,但是现在新版本的filezilla已经不支持windows2003了,所以趁机试一下iis配置ftp服务器. 前面都是很常规的配置 参考 ...

  10. MVC 删除文件

    protected void DeleteTempFiles(string iFileName) { FileInfo f = new FileInfo(iFileName); DirectoryIn ...