题目链接

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. SQL Server中timestamp(时间戳)

    SQL Server timestamp 数据类型与时间和日期无关.SQL Server timestamp 是二进制数字,它表明数据库中数据修改发生的相对顺序.实现 timestamp 数据类型最初 ...

  2. IOS_设置启动图片若干问题

    在做项目时, 发现设置了LaunchImage时发现一些问题: 1. 启动图片的设置可以通过两种方法: 1) 通过在LaunchScreen里放入ImageView 并设置图片, 这种方法的好处在于不 ...

  3. ASP.NET MVC4学习笔记之总体概述

    断断续续使用ASP.NET MVC框架也有一年多了,也算积累了一些经验,唉,一直想写一些笔记好好总结一下,人太懒不想动笔,今天终于决定开始.希望自己能坚持下去. 这篇文章大体介绍ASP.NET MVC ...

  4. 解决DataSnap支持的Tcp长连接数受限的两种方法

    如何解决DataSnap支持的Tcp长连接数受限的问题? 方案一: 采用代理服务器方式,基本流程为: 1.客户先连接代理服务器:2.获取可用的服务器IP和端口:3.关闭与代理服务器之间的连接:4.建立 ...

  5. How to begin Python learning?

    如何开始Python语言学习? 1. 先了解它,Wiki百科:http://zh.wikipedia.org/zh-cn/Python 2. Python, Ruby等语言来自开源社区,社区的学法是V ...

  6. django post报403问题

    第一个问题是: 我使用jquery的ajax向后台传值, 当使用GET方法时没问题 $.ajax({ type:"GET" url: data: success: }) 但是由于基 ...

  7. 一个简单WPF登陆界面,包含记住密码,自动登录等功能,简洁美观

    简介:这是一个自己以前用WPF设计的登陆界面,属于一个实验性的界面窗体,如果用于产品还很有不足.但也是有一点学习价值.后台代码略有复杂,但基本上都有注释 分类,略有代码经验的一般都能看懂. 登陆界面外 ...

  8. CentOS 6.3 配置 yum

    ContOS 配置yum:1.cd /etc/yum.repos.d2.创建个任意目录,将所有文件移动到创建的目录中,除了CentOS-Media.repo3.编辑CentOS-Media.repov ...

  9. POJ 3321 DFS序+线段树

    单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include < ...

  10. LintCode-Search 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...